2
You can combine the two forms in one:
class UserForm(forms.ModelForm):
class Meta:
model = User
class UserProfileForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
# magic
self.user = kwargs['instance'].user
user_kwargs = kwargs.copy()
user_kwargs['instance'] = self.user
self.user_form = UserForm(*args, **user_kwargs)
# magic end
super(UserProfileForm, self).__init__(*args, **kwargs)
self.fields.update(self.user_form.fields)
self.initial.update(self.user_form.initial)
def save(self, *args, **kwargs):
self.user_form.save(*args, **kwargs)
return super(ProfileForm, self).save(*args, **kwargs)
class Meta:
model = UserProfile
0
There are a few options here but i would just use a simple TemplateView or similar
Instantiate the forms in get and post with the appropriate params
def get(...):
self.user_form = UserForm(instance=user)
self.profile_form = ProfileForm(instance=profile)
return super().get(...)
def post(...)
self.user_form = UserForm(request.POST, instance=user)
self.profile_form = ProfileForm(request.POST, instance=profile)
if valid:
save_stuff()
return redirect()
return render_to_response(...) or super().get(...)
def get_context_data(self, **kwargs):
context = super(ProfileEdit, self).get_context_data(**kwargs)
context['forms'] = [self.user_form, self.profile_form]
return context
Then get_context_data will not handle logic for get or post
Source:stackexchange.com