Django - Use multiple models and forms for the same view

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!

Answers


I implemented a customized authentication. That solved it for me. Use this tutorial: https://docs.djangoproject.com/en/dev/topics/auth/customizing/#a-full-example


Need Your Help

Upload File to FTP Server on iPhone

iphone upload ftp

I would like to implement logic that uploads file to FTP Server.

Multidimensional ragged array of type that extends generic trait

arrays scala multidimensional-array initializer-list

Let's say I have a trait, Foo and this trait takes a generic type Bar so that the definition for Foo would look exactly like