[Answer]-Django editing model with fields from related model

1👍

You can use 2 forms on one page. Just choose different prefixes for the forms:

user_form = UserForm(prefix='user_form')
profile_form = UserForm(prefix='profile_form')

Just print them out in one html <form> tag:

<form>
    {{ user_form.as_p }}
    {{ profile_form.as_p }}
    <input type="submit"/>
</form>

Just validate each one, and save:

if user_form.is_valid() and profile_form.is_valid():
    user_form.save()
    profile_form.save()

EDIT: as Daniel Roseman pointed out, you might want to check is_valid() separately on each form so you will always see errors on both forms. Something like this:

user_valid = user_form.is_valid()
profile_valid = profile_form.is_valid()

if user_valid and profile_valid:
    user_form.save()
    profile_form.save()

Leave a comment