Ticket #12915: formfield_callback_inheritance.diff

File formfield_callback_inheritance.diff, 1.7 KB (added by semenov, 5 years ago)
  • django/forms/models.py

     
    179179
    180180class ModelFormMetaclass(type):
    181181    def __new__(cls, name, bases, attrs):
    182         formfield_callback = attrs.pop('formfield_callback',
    183                 lambda f: f.formfield())
     182        formfield_callback = attrs.pop('formfield_callback', None)
    184183        try:
    185184            parents = [b for b in bases if issubclass(b, ModelForm)]
    186185        except NameError:
     
    192191        if not parents:
    193192            return new_class
    194193
     194        # If formfield_callback is not present in the class definition,
     195        # copy it from the parent class.
     196        if not formfield_callback:
     197            for parent in parents:
     198                formfield_callback = getattr(parent, 'formfield_callback', None)
     199                if formfield_callback:
     200                    break
     201        if formfield_callback:
     202            new_class.formfield_callback = staticmethod(formfield_callback)
     203
    195204        if 'media' not in attrs:
    196205            new_class.media = media_property(new_class)
    197206        opts = new_class._meta = ModelFormOptions(getattr(new_class, 'Meta', None))
    198207        if opts.model:
    199208            # If a model is defined, extract form fields from it.
    200209            fields = fields_for_model(opts.model, opts.fields,
    201                                       opts.exclude, formfield_callback)
     210                                      opts.exclude, formfield_callback or (lambda f: f.formfield()))
    202211            # Override default model fields with any custom declared ones
    203212            # (plus, include all the other declared fields).
    204213            fields.update(declared_fields)
Back to Top