#14082: modelform_factory should use the form's metaclass
-------------------------------------+-------------------------------------
               Reporter:  jspiros    |        Owner:  Honza_Kral
                 Status:  reopened   |    Milestone:
              Component:  Forms      |      Version:  SVN
             Resolution:             |     Keywords:  ModelForm,
           Triage Stage:  Accepted   |  ModelFormMetaclass,
    Needs documentation:  0          |  modelform_factory
Patch needs improvement:  0          |    Has patch:  1
                                     |  Needs tests:  1
-------------------------------------+-------------------------------------
Changes (by melinath):

 * cc: melinath (added)
 * status:  closed => reopened
 * resolution:  worksforme =>


Comment:

 I've looked into this issue and there actually is a problem here.
 Essentially, the !ModelFormMetaclass will be run twice.

 Suppose I define the following modelform subclass:

 {{{
 #!python
 class MyModelFormMetaclass(ModelFormMetaclass):
     run = 0
     def __new__(*args, **kwargs):
         return ModelFormMetaclass.__new__(*args, **kwargs)

 class MyModelForm(ModelForm):
     __metaclass__ = MyModelFormMetaclass
 }}}

 If I use modelform_factory in its current state, the following calls
 occur:

 1. a call to !ModelFormMetaclass.!__new!__
 2. a super() call by !ModelFormMetaclass ![1]
 3. a call to !MyModelFormMetaclass.!__new!__ in the course of the super
 call. This is how type works, apparently.
 4. a call to !ModelFormMetaclass.!__new!__ during
 !MyModelFormMetaclass.!__new!__

 This should be corrected not only because it's inelegant to be calling
 !ModelFormMetaclass.!__new!__ twice and not only because it's odd that the
 code currently doesn't call the form's actual metaclass on principle, but
 also because this can cause real issues.

 Just to pick an example, if I try to modify the base_fields of the form in
 my custom metaclass *after* calling !ModelFormMetaclass myself, my changes
 will be overridden by !ModelFormMetaclass once the stack gets back up to
 it.

 ![1]
 http://code.djangoproject.com/browser/django/trunk/django/forms/models.py#L194

-- 
Ticket URL: <http://code.djangoproject.com/ticket/14082#comment:6>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to