Ticket #7837: forms_metaclass_hierarchy_problem_fix.diff

File forms_metaclass_hierarchy_problem_fix.diff, 3.6 KB (added by Manuel Saelices, 16 years ago)

This patch fix problems

  • django/forms/models.py

     
    230230
    231231        if 'media' not in attrs:
    232232            new_class.media = media_property(new_class)
    233         declared_fields = get_declared_fields(bases, attrs, False)
     233        if attrs.has_key('declared_fields'):
     234            declared_fields = attrs['declared_fields']
     235        else:
     236            declared_fields = SortedDict()
     237        declared_fields.update(get_declared_fields(bases, attrs, False))
    234238        opts = new_class._meta = ModelFormOptions(getattr(new_class, 'Meta', None))
    235239        if opts.model:
    236240            # 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

     
    1515    name = models.CharField(max_length=10)
    1616
    1717__test__ = {'API_TESTS': """
    18 >>> from django.forms import form_for_model, form_for_instance
     18>>> from django import forms
    1919
    2020# Boundary conditions on a PostitiveIntegerField #########################
    21 >>> BoundaryForm = form_for_model(BoundaryModel)
     21>>> BoundaryForm = forms.form_for_model(BoundaryModel)
    2222>>> f = BoundaryForm({'positive_integer':100})
    2323>>> f.is_valid()
    2424True
     
    3232# Formfield initial values ########
    3333If the model has default values for some fields, they are used as the formfield
    3434initial values.
    35 >>> DefaultsForm = form_for_model(Defaults)
     35>>> DefaultsForm = forms.form_for_model(Defaults)
    3636>>> DefaultsForm().fields['name'].initial
    3737u'class default value'
    3838>>> DefaultsForm().fields['def_date'].initial
     
    4343In form_for_instance(), the initial values come from the instance's values, not
    4444the model's defaults.
    4545>>> foo_instance = Defaults(name=u'instance value', def_date = datetime.date(1969, 4, 4), value = 12)
    46 >>> InstanceForm = form_for_instance(foo_instance)
     46>>> InstanceForm = forms.form_for_instance(foo_instance)
    4747>>> InstanceForm().fields['name'].initial
    4848u'instance value'
    4949>>> InstanceForm().fields['def_date'].initial
    5050datetime.date(1969, 4, 4)
    5151>>> InstanceForm().fields['value'].initial
    525212
     53
     54# Regression test for #6926 ##################################################
     55
     56Make sure hierarchy in metaclasses doesn't miss declared fields
     57
     58>>> class ExtraFieldForm(forms.Form):
     59...     extra_field = forms.CharField()
     60
     61>>> class MixinForm(ExtraFieldForm, forms.ModelForm):
     62...    # this is necessary to avoid python metaclass conflict error
     63...    __metaclass__ = type('FooFormMetaclass', (ExtraFieldForm.__metaclass__, forms.ModelForm.__metaclass__), {})
     64...    non_existing_field = forms.CharField()
     65...    class Meta:
     66...        model = ChoiceModel
     67
     68>>> MixinForm.base_fields.keys()
     69['name', 'extra_field', 'non_existing_field']
    5370"""}
Back to Top