Opened 10 years ago

Closed 10 years ago

#23824 closed Uncategorized (duplicate)

excluded fields on ModelForm does not work on inherited fields

Reported by: Ben Davis Owned by:
Component: Forms Version: 1.7
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When you have a ModelForm subclass, any fields in the exclude array are still included on the form if the field is part of the parent class. This happens in ModelFormMetaClass.__new__ when the class overrides new_class.base_fields with the parent fields *after* applying the exclusions.

Change History (5)

comment:1 by Ola Sitarska, 10 years ago

Owner: changed from nobody to Ola Sitarska
Status: newassigned

comment:2 by Ola Sitarska, 10 years ago

Owner: Ola Sitarska removed
Status: assignednew

Together with Baptiste and Loic, I managed to reproduce that, but we've decided that this is an expected behaviour. It only happens when you actually declare a field in a model form:

from django import forms

from .models import SampleModel

class BaseForm(forms.ModelForm):
    number = forms.CharField()

    class Meta:
        model = SampleModel

class SubclassForm(BaseForm):

    class Meta(BaseForm.Meta):
        exclude = ['number',]

Then you can see a number field in the form, but it doesn't get saved. Should I document it somewhere?

Last edited 10 years ago by Ola Sitarska (previous) (diff)

comment:3 by Danilo Bargen, 10 years ago

This would be the test to reproduce the issue:

  • tests/model_forms/tests.py

    a b class ModelFormBaseTest(TestCase):  
    473473    def test_subclassmeta_form(self):
    474474        class SomeCategoryForm(forms.ModelForm):
    475475            checkbox = forms.BooleanField()
     476            othertext = forms.CharField()
    476477
    477478            class Meta:
    478479                model = Category
    class ModelFormBaseTest(TestCase):  
    483484            list.
    484485            """
    485486            class Meta(SomeCategoryForm.Meta):
    486                 exclude = ['url']
     487                exclude = ['url', 'othertext']
    487488
    488489        self.assertHTMLEqual(
    489490            str(SubclassMeta()),

What's the reasoning for considering this intentional?

Last edited 10 years ago by Danilo Bargen (previous) (diff)

comment:4 by Tim Graham, 10 years ago

I think the documentation was added in #8620, but perhaps it needs further clarification?

comment:5 by Tim Graham, 10 years ago

Resolution: duplicate
Status: newclosed

Closing as duplicate unless reporter can provide additional information as to why the current documentation is insufficient (I'd guess he probably didn't see it).

Note: See TracTickets for help on using tickets.
Back to Top