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!


This error happens because you are using the UpdateView generic view. This class expects a single model and form. As a solution, you could override the UpdateView methods to take the two models into account like you did with get_context_data.

I implemented a customized authentication. That solved it for me. Use this tutorial:

Need Your Help

Need more than one of the same jQuery effect on same page

javascript jquery scroll

So I'm using this jquery background scroller, basically I want to get more than two on the same page (going at different speeds) and I can't figure out how to do it.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.