Is it possible to have multiple models included in a single ModelForm in django? I am trying to create a profile edit form. So I need to include some fields from the User model and the UserProfile model. Currently I am using 2 forms like this
class UserEditForm(ModelForm):
class Meta:
model = User
fields = ("first_name", "last_name")
class UserProfileForm(ModelForm):
class Meta:
model = UserProfile
fields = ("middle_name", "home_phone", "work_phone", "cell_phone")
Is there a way to consolidate these into one form or do I just need to create a form and handle the db loading and saving myself?
You can just show both forms in the template inside of one
<form>html element. Then just process the forms separately in the view. You’ll still be able to useform.save()and not have to process db loading and saving yourself.In this case you shouldn’t need it, but if you’re going to be using forms with the same field names, look into the
prefixkwarg for django forms. (I answered a question about it here).