Ticket #7837: forms_metaclass_hierarchy_problem_fix_r8335.diff

File forms_metaclass_hierarchy_problem_fix_r8335.diff, 2.4 KB (added by Manuel Saelices, 16 years ago)

Patch updated for [8335]

  • django/forms/models.py

     
    168168
    169169        if 'media' not in attrs:
    170170            new_class.media = media_property(new_class)
    171         declared_fields = get_declared_fields(bases, attrs, False)
     171        if attrs.has_key('declared_fields'):
     172            declared_fields = attrs['declared_fields']
     173        else:
     174            declared_fields = SortedDict()
     175        declared_fields.update(get_declared_fields(bases, attrs, False))
    172176        opts = new_class._meta = ModelFormOptions(getattr(new_class, 'Meta', None))
    173177        if opts.model:
    174178            # If a model is defined, extract form fields from it.
  • django/forms/forms.py

     
    5656    'base_fields', taking into account parent class 'base_fields' as well.
    5757    """
    5858    def __new__(cls, name, bases, attrs):
    59         attrs['base_fields'] = get_declared_fields(bases, attrs)
     59        declared_fields = get_declared_fields(bases, attrs)
     60        attrs['declared_fields'] = declared_fields
     61        attrs['base_fields'] = declared_fields
    6062        new_class = super(DeclarativeFieldsMetaclass,
    6163                     cls).__new__(cls, name, bases, attrs)
    6264        if 'media' not in attrs:
  • tests/regressiontests/forms/models.py

     
    7373datetime.date(1969, 4, 4)
    7474>>> instance_form.initial['value']
    757512
     76
     77
     78# Regression test for #7837 ##################################################
     79
     80Make sure hierarchy in metaclasses doesn't miss declared fields
     81
     82>>> class ExtraFieldForm(django_forms.Form):
     83...     extra_field = django_forms.CharField()
     84
     85>>> class ChildForm(ExtraFieldForm, ModelForm):
     86...    # this is necessary to avoid python metaclass conflict error
     87...    __metaclass__ = type('ChildFormMetaclass', (ExtraFieldForm.__metaclass__, django_forms.ModelForm.__metaclass__), {})
     88...    non_existing_field = django_forms.CharField()
     89...    class Meta:
     90...        model = ChoiceModel
     91
     92>>> ChildForm.base_fields.keys()
     93['name', 'extra_field', 'non_existing_field']
    7694"""}
Back to Top