Ticket #7837: forms_metaclass_hierarchy_problem_fix_r8049.diff

File forms_metaclass_hierarchy_problem_fix_r8049.diff, 3.9 KB (added by Manuel Saelices, 16 years ago)

Updated patch for [8049]

  • django/forms/models.py

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

     
    2525    file1 = django_forms.FileField()
    2626
    2727__test__ = {'API_TESTS': """
    28 >>> from django.forms import form_for_model, form_for_instance
    2928>>> from django.core.files.uploadedfile import SimpleUploadedFile
    3029
    3130# FileModel with unicode filename and data #########################
     
    3736>>> m = FileModel.objects.create(file=f.cleaned_data['file1'])
    3837
    3938# Boundary conditions on a PostitiveIntegerField #########################
    40 >>> BoundaryForm = form_for_model(BoundaryModel)
     39>>> BoundaryForm = django_forms.form_for_model(BoundaryModel)
    4140>>> f = BoundaryForm({'positive_integer':100})
    4241>>> f.is_valid()
    4342True
     
    5150# Formfield initial values ########
    5251If the model has default values for some fields, they are used as the formfield
    5352initial values.
    54 >>> DefaultsForm = form_for_model(Defaults)
     53>>> DefaultsForm = django_forms.form_for_model(Defaults)
    5554>>> DefaultsForm().fields['name'].initial
    5655u'class default value'
    5756>>> DefaultsForm().fields['def_date'].initial
     
    6261In form_for_instance(), the initial values come from the instance's values, not
    6362the model's defaults.
    6463>>> foo_instance = Defaults(name=u'instance value', def_date = datetime.date(1969, 4, 4), value = 12)
    65 >>> InstanceForm = form_for_instance(foo_instance)
     64>>> InstanceForm = django_forms.form_for_instance(foo_instance)
    6665>>> InstanceForm().fields['name'].initial
    6766u'instance value'
    6867>>> InstanceForm().fields['def_date'].initial
    6968datetime.date(1969, 4, 4)
    7069>>> InstanceForm().fields['value'].initial
    717012
     71
     72# Regression test for #7837 ##################################################
     73
     74Make sure hierarchy in metaclasses doesn't miss declared fields
     75
     76>>> class ExtraFieldForm(django_forms.Form):
     77...     extra_field = django_forms.CharField()
     78
     79>>> class ChildForm(ExtraFieldForm, django_forms.ModelForm):
     80...    # this is necessary to avoid python metaclass conflict error
     81...    __metaclass__ = type('ChildFormMetaclass', (ExtraFieldForm.__metaclass__, django_forms.ModelForm.__metaclass__), {})
     82...    non_existing_field = django_forms.CharField()
     83...    class Meta:
     84...        model = ChoiceModel
     85
     86>>> ChildForm.base_fields.keys()
     87['name', 'extra_field', 'non_existing_field']
    7288"""}
Back to Top