Ticket #3815: subclass_form.2.diff

File subclass_form.2.diff, 1.4 KB (added by Chris Beaven, 17 years ago)

safer: makes sure base_fields is a dict instance

  • django/newforms/forms.py

     
    4848        for base in bases[::-1]:
    4949            if hasattr(base, 'base_fields'):
    5050                fields = base.base_fields.items() + fields
     51        # If this is a model_for_* form, it could also have a base_fields
     52        # attribute.
     53        if 'base_fields' in attrs and isinstance(attrs['base_fields'], dict):
     54            fields += attrs['base_fields'].items()
    5155
    5256        attrs['base_fields'] = SortedDictFromList(fields)
    5357        return type.__new__(cls, name, bases, attrs)
  • tests/modeltests/model_forms/models.py

     
    560560True
    561561>>> f.cleaned_data
    562562{'phone': u'312-555-1212', 'description': u'Assistance'}
     563
     564# Subclassing using Form ######################################################
     565
     566>>> CategoryForm = form_for_model(Category, form=Form)
     567>>> print CategoryForm().base_fields.keys()
     568['name', 'slug', 'url']
     569>>> class CategoryWithCommentForm(CategoryForm):
     570...     comment = CharField()
     571>>> print CategoryWithCommentForm().base_fields.keys()
     572['name', 'slug', 'url', 'comment']
    563573"""}
Back to Top