How to do 2-ModelForm-in-1-CVB right?

33 views
Skip to first unread message

Adam

unread,
Sep 26, 2013, 1:19:41 AM9/26/13
to django...@googlegroups.com
Hi,

I am trying to handle 2 ModelForm in 1 view. Is that possible with CBV solution? Does any best practise exist?

Here is the problem in details.

## models.py

class UserProfile(HasJPAddresses, models.Model):

    user = models.OneToOneField(User)
    birthdate = models.DateField(default='1900-01-01')

## forms.py

class UserProfileUpdateForm(forms.ModelForm):
    class Meta:
        model = UserProfile

class UserUpdateForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ['first_name', 'last_name']

## views.py

class UserProfileUpdateView(FormView): <<<<<<<---- How could I handle two forms at once in a view?

Thanks,
/Adam

Yu Chen

unread,
Sep 26, 2013, 9:03:09 PM9/26/13
to django...@googlegroups.com
Inlineformset may solve your problem, and this http://haineault.com/blog/155/ is a good example.


在 2013年9月26日星期四UTC+8下午1时19分41秒,Adam写道:

Adam

unread,
Sep 27, 2013, 5:26:21 AM9/27/13
to django...@googlegroups.com
2013年9月27日金曜日 10時03分09秒 UTC+9 Yu Chen:
Inlineformset may solve your problem, and this http://haineault.com/blog/155/ is a good example.


In the sample code, CBV has to attached to FormView and has to provide one 'form_class' member. Another extra form is set by context_data. It is still not perfect. For example, consider a dynamic formset with which you can create new objects by adding new forms in UI. In this case, it is not easy to handle all forms in one view.

/Adam

 
Reply all
Reply to author
Forward
0 new messages