I created a UserProfile model and I want to connect it with the django-built-in User model. In my case, I want to update UserProfile information and User information at the same time. My UserProfile model:
class UserProfile(models.Model):
user = models.OneToOneField(User)
street = models.TextField(max_length=100, null=True)
My forms:
class UserForm(ModelForm):
class Meta:
model = User
fields = ['first_name', 'last_name', 'email']
widgets = {
'first_name': TextInput(attrs={'required': True, 'size': 28}),
'last_name': TextInput(attrs={'required': True, 'size': 28}),
'email': EmailInput(attrs={'required': True, 'size': 28}),
}
class UserProfileForm(ModelForm):
class Meta:
model = UserProfile
fields = ['street']
widgets = {
'street': TextInput(attrs={'required': False, 'placeholder': ' Street'}),
}
And my view:
class UpdateUserAccountView(SuccessMessageMixin, UpdateView):
model = User
template_name = 'dashboard/update_user_account.html'
success_message = 'Profile successfully updated'
form_class = UserForm
success_url = '/dashboard/account'
def get_context_data(self, **kwargs):
context = super(UpdateUserAccountView, self).get_context_data(**kwargs)
context['title'] = 'Update Profile'
context['user'] = User.objects.all()
context['userprofile'] = UserProfile.objects.all()
return context
I can update all information that belongs to the User, not to the UserProfile. However, if I change
model = User
to model = UserProfile
and form_class = UserForm
to form_class = UserProfileForm
I can update the street, but not first_name, last_name or email.
Therefore I need a connection between those models and forms.
I tried this:
model = User, UserProfile
form_class = UserForm, UserProfileForm
but then I get this error:
'tuple' object has no attribute '_default_manager'
Please help!