ModelFormset Into InlineFormset

25 views
Skip to first unread message

Matthew Pava

unread,
May 31, 2017, 6:14:44 PM5/31/17
to django...@googlegroups.com

I have a class CustomFormSet that inherits from BaseModelFormSet.  I would like to have another formset class that does everything CustomFormSet does but instead inherits from BaseInlineFormSet.  What is the best pythonic/Django way of doing that?

 

James Schneider

unread,
May 31, 2017, 7:24:35 PM5/31/17
to django...@googlegroups.com
On Wed, May 31, 2017 at 3:13 PM, Matthew Pava <Matthe...@iss.com> wrote:

I have a class CustomFormSet that inherits from BaseModelFormSet.  I would like to have another formset class that does everything CustomFormSet does but instead inherits from BaseInlineFormSet.  What is the best pythonic/Django way of doing that?

 


I would take the same approach that abstract model classes use. Collect the common functionality into a separate class, and mix the forms together with the desired inheritance hierarchy.

Since your inheritance hierarchy includes the Base*FormSet classes, your abstract form mixin can simply inherit from object to keep from adding unintended behavior. 

-James  

Matthew Pava

unread,
Jun 1, 2017, 4:35:36 PM6/1/17
to django...@googlegroups.com

Well, that took me on a path to enlightenment.  I learned quite a bit more about method resolution order, MRO.  On my journey, I also learned the new way of using super() in python 3.  I even stumbled upon type checking using mypy.  And I learned that there are stub files available for Django with mypy.

 

Unfortunately, my CustomModelFormSet still does not work with one particular form/view.  It works in other form/views, though, so there’s a problem elsewhere to resolve.

 

Thank you for your insight, James!  It was most helpful!

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciX9xDt3moEQPGdgy3SBt3xvPV3z3eUGun6eq%3DMSPKh_hQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

James Schneider

unread,
Jun 1, 2017, 5:29:32 PM6/1/17
to django...@googlegroups.com
On Thu, Jun 1, 2017 at 1:34 PM, Matthew Pava <Matthe...@iss.com> wrote:

Well, that took me on a path to enlightenment.  I learned quite a bit more about method resolution order, MRO.  On my journey, I also learned the new way of using super() in python 3.  I even stumbled upon type checking using mypy.  And I learned that there are stub files available for Django with mypy.

 

Unfortunately, my CustomModelFormSet still does not work with one particular form/view.  It works in other form/views, though, so there’s a problem elsewhere to resolve.

 

Thank you for your insight, James!  It was most helpful!


Good to hear! I sometimes wonder if I'm actually helping people on this list. I've been wrong more than once. ;-)

Post up the problem view with a description of the problem in a separate thread and maybe someone can help you out. I haven't needed to work with form sets so I don't have any general nuggets of wisdom.

-James
Reply all
Reply to author
Forward
0 new messages