1
votes

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!

2
Note that a separate UserProfile is no longer recommended since 1.5, as you can now define a custom User subclass that includes all your fields. - Daniel Roseman
Hello, thank you for your help. I will try it. - user3083117

2 Answers

0
votes

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

-1
votes

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.