Answer the question
In order to leave comments, you need to log in
How to link a user object to another object while saving it?
Hello!
I have an authorized user, there is a form that he must fill out after registration and authorization, the data from the form is saved in the database and there is a foreign key associated with the User table, how to make it automatically dependency, for example, Vasya is authorized and enters the necessary data and when he clicks save. that the table was saved at once tied to his account? on the screenshot indicated how it should be after saving the data!
class UserdData(models.Model):
username = models.ForeignKey(User,blank=True,null=True,default=None)
first_name = models.CharField(max_length=100,blank=True,null=True)
last_name = models.CharField(max_length=100,blank=True,null=True)
city=models.CharField(max_length=100,blank=True,null=True)
street= models.CharField(max_length=100,blank=True,null=True)
building = models.CharField(max_length=5,blank=True,null=True)
index = models.CharField(max_length=10,default=None,blank=True,null=True)
postcode=models.IntegerField(max_length=10,blank=True,null=True)
phone = models.CharField(max_length=30,default=None,blank=True,null=True)
def save_user(self):
self.save()
def __str__(self):
return '%s %s' % (self.last_name,self.first_name)
@csrf_protect
@login_required
def user_data(request):
if request.method == "POST":
form = UserDataForm(request.POST)
if form.is_valid():
form.save()
return redirect('myoffice/account/userpage.html')
else:
form=UserDataForm()
return render(request,'myoffice/userdata.html',{'form':form})
class UserDataForm(forms.ModelForm):
class Meta:
model = UserdData
fields=('first_name','last_name', 'city','street','building','index','postcode','phone')
Answer the question
In order to leave comments, you need to log in
1. The model django.contrib.auth.models.User
already has fields first_name
and last_name
- additional copies will not bring clarity, on the contrary, sooner or later a mess will form in the database and it will not be clear where the real name and surname are, and the meaning of such denormalization is generally unclear
2. It is not clear why you need the method save_user
- it is nothing does not and is absolutely not needed
3. It is more correct to name the field user
instead username
of 4. Theoretically (depending on your requirements) it is more correct to create your own user model and refer to it as settings.AUTH_USER_MODEL
5. In the definition of fields with null=True, blank=True
is not needed default
- by default it will be None
6. Strange model, in which all fields can be NULL - rather, this is from misunderstanding
7. For your purposes, it is more correct to use django.views.generic.edit.CreateView, django.views.generic.edit.UpdateView, and the code will be shorter, and even the form can not be defined
8. redirect
Some kind of game is written in it!
9. When using the default settings, all views are already protected from CSRF, there is no need to explicitly write decorator csrf_protect
A in general, you should at least start by reading the Django Tutorial. You are clearly writing code without understanding what you are writing and why . Hence the problems.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question