Ticket #13971: 13971_patch.diff

File 13971_patch.diff, 3.0 KB (added by matiasb, 14 years ago)

Possible patch that filters out not included (or excluded) fields from declared fields

  • django/forms/models.py

     
    196196        )
    197197    return field_dict
    198198
     199def filter_declared_fields(model, declared_fields, fields=None, exclude=None):
     200    """
     201    Returns an updated ``SortedDict`` filtering out the model fields
     202    not included or excluded in the form definition.
     203
     204    """
     205    opts = model._meta
     206    for f in opts.fields + opts.many_to_many:
     207        exclude_field = (fields is not None and not f.name in fields) or \
     208                        (exclude is not None and f.name in exclude)
     209        if exclude_field and f.name in declared_fields:
     210            del declared_fields[f.name]
     211    return declared_fields
     212
     213
    199214class ModelFormOptions(object):
    200215    def __init__(self, options=None):
    201216        self.model = getattr(options, 'model', None)
     
    234249                message = message % (', '.join(missing_fields),
    235250                                     opts.model.__name__)
    236251                raise FieldError(message)
     252            # Filter out excluded fields from declared fields
     253            declared_fields = filter_declared_fields(opts.model,
     254                                                     declared_fields,
     255                                                     opts.fields, opts.exclude)
    237256            # Override default model fields with any custom declared ones
    238257            # (plus, include all the other declared fields).
    239258            fields.update(declared_fields)
  • tests/regressiontests/model_forms_regress/tests.py

     
    442442        self.assertTrue(form.is_valid())
    443443        instance = construct_instance(form, Person(), fields=())
    444444        self.assertEqual(instance.name, '')
     445
     446
     447class BasePublicationForm(forms.ModelForm):
     448    date_published = forms.CharField()
     449    class Meta:
     450        model = Publication
     451
     452class PublicationFormWithFields(BasePublicationForm):
     453    class Meta(BasePublicationForm.Meta):
     454        fields = ('title',)
     455
     456class PublicationFormWithExclude(BasePublicationForm):
     457    class Meta(BasePublicationForm.Meta):
     458        exclude = ('date_published',)
     459       
     460class ExcludeCustomizedField(TestCase):
     461    """Test excluded form customized model field is not added to form (#13971)."""
     462   
     463    def test_field_is_not_added(self):
     464        form = PublicationFormWithFields()
     465        self.assert_('title' in form.fields)
     466        self.assert_('date_published' not in form.fields)
     467        self.assertEqual(len(form.fields), 1)
     468 
     469    def test_field_is_excluded(self):
     470        form = PublicationFormWithExclude()
     471        self.assert_('title' in form.fields)
     472        self.assert_('date_published' not in form.fields)
     473        self.assertEqual(len(form.fields), 1)
     474
Back to Top