[Answered ]-Can't update User and UserProfile in one View?

2👍

UpdateView is only made to handle one model with no relations. However, the wonderful django-extra-views library provides CBVs for models and inline relations.

class UserProfileInline(InlineFormSet):
    model = models.UserProfile
    form = UserProfileUpdateForm
    extra = 0
    def get_factory_kwargs(self):
        kwargs = super(UserProfileInline,self).get_factory_kwargs()
        kwargs.update({"min_num": 1})
        return kwargs

class UserCreate(CreateWithInlinesView):
    model=User
    inlines = [UserProfileInline]
    form_class = UserForm
    success_url = reverse('some-success-url')
    # REQUIRED - fields or exclude fields of User model
    template_name = 'your_app/user_profile_update.html'

Be sure to check out the documentation for information on the variables passed to your template and how to work with inline formsets.

0👍

You have to create second form for User as well. Then pass it to the same UpdateView as a second form_class.

Note*: you may need to override get and post methods for UpdateView. This SO answer might help.

Render both forms in one template under one <form> tag:

<form action="" method="post">
    {% csrf_token %}
    {{ first_form }}
    {{ second_form }}
</form>

Leave a comment