I am trying to store user details with the PhoneBook and Contacts he creates so that I will be able to show the PhoneBook and Contacts that have been created by them.
I don't want the user to explicitly add the details himself. It should be handled by the backend.
I am successfully able to store the user details with the PhoneBook he created, but when I try to do the same thing with contacts, I am getting an attribute error.
models.py
class PhoneBook(models.Model):
name = models.CharField(max_length=10, blank=False)
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
def __str__(self):
return self.name
class Contact(models.Model):
first_name = models.CharField(max_length=50, blank=False)
last_name = models.CharField(max_length=50, blank=False)
phone_number = models.CharField(max_length=13, blank=False, unique=True)
phone_book = models.ManyToManyField(PhoneBook, related_name='phone_book')
user = models.ForeignKey(User, on_delete=models.SET_NULL, null=True)
def __str__(self):
return self.phone_number
views.py
#login_required
def create_phone_book(request):
form = CreatePhoneBookForm(request.POST or None)
form.instance.user = request.user
if form.is_valid():
form.save()
form = CreatePhoneBookForm()
context = {'form': form}
return render(request, 'CallCenter/create_phone_book.html', context)
#login_required
def add_to_phone_book(request):
form = AddToPhoneBookForm(request.POST or None)
form.instance.user = request.user
if form.is_valid():
form.save()
form = AddToPhoneBookForm()
context = {'form': form}
return render(request, 'CallCenter/add_to_phone_book.html', context)
forms.py
class AddToPhoneBookForm(forms.Form):
class Meta:
model = Contact
fields = ['first_name', 'last_name', 'phone_number', 'phone_book']
class CreatePhoneBookForm(forms.Form):
class Meta:
model = PhoneBook
fields = ['name']
The error I am getting is
AttributeError at /call-center/add/
'AddToPhoneBookForm' object has no attribute 'instance'
You should make use of a ModelForm, not a Form. A Form itself has no instance, so you can define:
class AddToPhoneBookForm(forms.ModelForm):
class Meta:
model = Contact
fields = ['first_name', 'last_name', 'phone_number', 'phone_book']
class CreatePhoneBookForm(forms.ModelForm):
class Meta:
model = PhoneBook
fields = ['name']
Usually when you make a successful post request, you should redirect, to implement the Post/Redirect/Get pattern [wiki].
Related
I'm doing reviews on django, but I want the user to not be able to enter any name. I want the username in the reviews to match the username of his profile
models.py
class Reviews(models.Model):
name = models.CharField('Имя', max_length=100)
text = models.TextField('Отзыв', max_length=3400)
parent = models.ForeignKey('self', verbose_name='Родитель', on_delete=models.SET_NULL, null=True, blank=True)
book = models.ForeignKey(BookModel, verbose_name='книга', on_delete=models.CASCADE)
name_user = models.ForeignKey(User, on_delete=models.CASCADE, null=True)
views.py
class MoreInfoView(View):
""" """
def get(self, request, id):
book_info = BookModel.objects.filter(id=id).first()
stuff = get_object_or_404(BookModel, id=self.kwargs['id'])
total_likes = stuff.total_likes()
return render(request, 'bookapp/more_info.html', context={
'id': id,
'book_info': book_info,
'book': BookModel.objects.all(),
'total_likes': total_likes,
})
class AddReview(View):
"""Add Review"""
def post(self, request, pk):
form = ReviewForm(request.POST)
book = BookModel.objects.get(id=pk)
if form.is_valid():
form = form.save(commit=False)
form.book = book
form.save()
return HttpResponseRedirect(reverse('more_info', args=[pk]))
forms
class ReviewForm(forms.ModelForm):
class Meta:
model = Reviews
fields = ("name", "text", 'name_user')
You can add user manually after validating ReviewForm
I also added some changes(suggestions)
models.py
class Reviews(models.Model):
name = models.CharField('Имя', max_length=100)
text = models.TextField('Отзыв', max_length=3400)
parent = models.ForeignKey('self', verbose_name='Родитель', on_delete=models.SET_NULL, null=True, blank=True)
book = models.ForeignKey(BookModel, verbose_name='книга', on_delete=models.CASCADE, blank=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, blank=True)
Setting blank=True makes the field optional.
views.py
class MoreInfoView(View):
""" """
def get(self, request, id):
book_info = BookModel.objects.filter(id=id).first()
stuff = get_object_or_404(BookModel, id=self.kwargs['id'])
total_likes = stuff.total_likes()
return render(request, 'bookapp/more_info.html', context={
'id': id,
'book_info': book_info,
'book': BookModel.objects.all(),
'total_likes': total_likes,
})
class AddReview(View):
"""Add Review"""
def post(self, request, pk):
user = request.user
# User has to be authenticated to create a review. And backend must
# validate it. You should raise PermissionDenied as response or
# redirect user to the login page, or something similar.
if not request.user.is_authenticated:
raise PermissionDenied()
form = ReviewForm(request.POST)
book = BookModel.objects.get(id=pk)
if form.is_valid():
form = form.save(commit=False)
form.book = book
form.user = user
form.save()
return HttpResponseRedirect(reverse('more_info', args=[pk]))
forms.py
class ReviewForm(forms.ModelForm):
class Meta:
model = Reviews
fields = ("name", "text")
I would advise to work with a CreateView [Django-doc] that will simplify a lot of the logic. You can implement this as:
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import CreateView
class AddReviewView(LoginRequiredMixin, CreateView):
form_class = ReviewForm
def get_success_url(self):
return reverse('more_info', args=[self.kwargs['pk']])
def form_valid(self, form):
form.instance.book_id = self.kwargs['pk']
form.name_user = self.request.user
return super().form_valid(form)
In your ReviewForm you thus remove the name_user as fields element.
Note: It is normally better to make use of the settings.AUTH_USER_MODEL [Django-doc] to refer to the user model, than to use the User model [Django-doc] directly. For more information you can see the referencing the User model section of the documentation.
Note: You can limit views to a class-based view to authenticated users with the
LoginRequiredMixin mixin [Django-doc].
Note: normally a Django model is given a singular name, so Review instead of Reviews.
Note: Models normally have no …Model suffix. Therefore it might be better to rename BookModel to Book.
I'm running my project on Python 3.7.5, Django >=2.2.8,<3.0.0 and Postgres 12.1
I have implemented 2 models:
class CustomUser(AbstractUser):
objects = CustomUserManager()
person = models.ForeignKey(
'Person', on_delete=models.DO_NOTHING, null=True)
class Person(models.Model):
uuid = models.UUIDField(primary_key=True, db_column='person_uuid')
first_name = models.CharField(max_length=255)
last_name = models.CharField(max_length=255)
address1 = models.CharField(max_length=255, blank=True, null=True)
address2 = models.CharField(max_length=255, blank=True, null=True)
add_date = models.DateTimeField(auto_now_add=True)
mod_date = models.DateTimeField(auto_now=True)
The CreateUser view shown below is for registering new users. This view consists of 2 forms, a user creation form and a person form. Once the user enters their details I want to save the person_form and set the person field in CustomUser to the newly created person. But the value of pk after save is always None.
To work around this, I query the Person model with the matching first_name and last_name and use the UUID from the response. But this is very brittle, as I have other models that use UUID and don't have unique values like first_name and last_name. I wonder if there is a more reliable way of getting the UUID of the form just saved.
class CreateUserView(View):
template_name = 'core/create_user.html'
def get(self, request, *args, **kwargs):
user_form = CustomUserCreationForm()
person_form = PersonForm()
context = {'person_form': person_form,
'user_form': user_form}
return render(request, self.template_name, context=context)
def post(self, request, *args, **kwargs):
person_form = PersonForm(request.POST)
user_form = CustomUserCreationForm(request.POST)
print('Person form is valid: {}'.format(person_form.is_valid()))
if person_form.is_valid() and user_form.is_valid():
print('User form is valid')
person = person_form.save()
print(f'PK of new person is: {person.pk}')
p = Person.objects.filter(first_name=person.first_name,
last_name=person.last_name).latest('add_date')
user = user_form.save(commit=False)
user.person = p
user.save()
messages.success(request, 'Account created successfully')
return redirect('login')
else:
return render(request, self.template_name, {'person_form': person_form,
'user_form': user_form})
Note: I realize that Person and CustomUser can be a single model, but Person is used elsewhere in the database with other entities and needs to be independent of user
I know this is late, but you might find this interesting, and also this. It seems you don't get any return because your primary key is no AutoField.
first post here. Im trying to query the user so i from the form so i can save the user in my database.
This is my code
form = lageBruker(request.POST)
bruker = User.objects.create(format(request.POST['username']))
print()
if request.method == 'POST':
if form.is_valid():
fornavn = request.POST['first_name']
etternavn = request.POST['last_name']
email = request.POST['email']
kunde = Kunde.objects.create(
#bruker = bruker,
fornavn=fornavn,
etternavn=etternavn,
email=email
)
kunde.save()
context = {'form': form}
return render(request, 'ebutikk/registrer.html', context)
Kunde Model:
class Kunde(models.Model):
bruker = models.OneToOneField(
User, null=True, blank=True, on_delete=models.CASCADE)
fornavn = models.CharField(max_length=200, null=True, blank=True)
etternavn = models.CharField(max_length=200, null=True, blank=True)
email = models.CharField(max_length=200, null=True, blank=True)
def __str__(self):
return self.etternavn
What im trying to do is, when the registration form is submittet, i want to save the registerd user as a kunde/customer. I dont have a User model, i use the default made by django with the import:
from django.contrib.auth.models import User
I make the form by importing the user creation form given by django and customizing it im my forms.py file.
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
class lageBruker(UserCreationForm):
class Meta:
model = User
fields = ['username',
'email',
'first_name',
'last_name',
'password1',
'password2']
Normally, you'd simply create the Kunde model like this:
class Kunde(models.Model):
bruker = models.OneToOneField(
User, null=True, blank=True, on_delete=models.CASCADE)
# more fields here, so the model makes sense
#property
def fornavn(self): return self.user.first_name
#property
def etternavn(self): return self.user.last_name
#property
def email(self): return self.user.email
Then all you need to do is:
def customer_signup_view(self, request):
if request.method == "POST":
form = lageBruker(request.POST)
# Saving the model form returns the user created
user = form.save()
# Now use that user to create a new customer
Kunde.objects.create(user=user)
return redirect("success/")
return render(request, 'ebutikk/registrer.html', {"form": lageBruker())
I want to create a user into auth_user. And use its id(primary key) to
fill in an entry into User_Profile to take it as a Foreign key.
Models.py:
class User_Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
contact_number = models.IntegerField()
birth_date = models.DateField(null=False)
address = models.CharField(max_length=200)
role = models.ForeignKey(Role, on_delete=models.CASCADE)
Forms.py:
class UserForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput)
class Meta:
model = User
fields = ['username', 'email', 'password']
class UserProfileForm(forms.ModelForm):
class Meta:
model = User_Profile
fields = [ 'contact_number', 'birth_date', 'address', 'role']
Views.py:
def registration_view(request):
form = UserForm(request.POST)
form2 = UserProfileForm(request.POST)
else:
context = {
'form': form,
'form2': form2
}
return render(request, 'Schoool/registration_form.html', context)
I am trying to make a webapp that includes register/login operations. The login is working fine but the registration form is frustrating.
I have a class registration form that inherits from UserCreationForm. The users are created and seen in the admin page when created but the problem I am having is not being able to see them in my UserProfile model in the admin page. It does not link the information and I could not find a way the link them.
Here is the registration form:
from django import forms
from django.contrib.auth.models import User
from django.contrib.auth.forms import UserCreationForm
class RegistrationForm(UserCreationForm):
email = forms.EmailField(required=True)
city = forms.CharField(required=False)
country = forms.CharField(required=True)
first_name = forms.CharField(required=True)
last_name = forms.CharField(required=True)
username = forms.CharField(required=True)
class Meta:
model = User
fields = (
'username',
'first_name',
'last_name',
'country',
'city',
'email'
)
def save(self, commit=True):
user = super(RegistrationForm, self).save(commit=False)
#user.first_name = self.cleaned_data['first_name']
if commit:
user.save()
return user
Here are my models:
class UserProfile(models.Model):
user = models.OneToOneField( User,
on_delete=models.CASCADE
)
username = models.TextField(max_length=30, default="")
first_name = models.TextField(max_length=30, default="")
last_name = models.TextField(max_length=30, default="")
country = models.TextField(max_length=30, default="Which country are you from?")
city = models.TextField(max_length=30, default="Which city are you from?")
class ColorChoice(models.Model):
user = models.ForeignKey(
'UserProfile',
on_delete=models.CASCADE
)
color1 = models.IntegerField()
color2 = models.IntegerField()
color3 = models.IntegerField()
color4 = models.IntegerField()
color5 = models.IntegerField()
and my view.py that does the registering:
def register(request):
if request.method == 'POST':
form = RegistrationForm(request.POST)
if form.is_valid():
form.save()
return redirect('color')
else:
form = RegistrationForm()
args = {'form': form}
return render(request, 'account/create_new.html', args)
Your form is only creating a new objects for User. You would need to add a signal for example to create a new object for UserProfile.
Something like this:
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
signals.post_save.connect(create_user_profile, sender=User)