Opened 9 years ago

Closed 9 years ago

#25679 closed Bug (invalid)

ModelForm.Meta.fields and exclude are not honoured for custom fields

Reported by: Tino de Bruijn Owned by: nobody
Component: Forms Version: dev
Severity: Normal Keywords: modelform
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no
Pull Requests:5552 unmerged

Description

Selecting fields to use states that only form fields that are listed in Meta.fields or are not listed in Meta.exclude are used and "Any fields not included in a form by the above logic will not be set by the form’s save() method.".

But when I add a custom field, it is displayed and saved even when not included in fields or when excluded. Eg.:

class TestForm(forms.ModelForm):
    conditional_field = forms.TextField()

    class Meta:
        model = MyModel
        exclude = ('conditional_field',)

Still displays "conditional_field".

The reason I wanted to use this, is conditionally toggle the field in __init__. (Even if that is not the correct way to approach this, it still is unexpected behaviour)

Change History (3)

by Hasan Ramezani, 9 years ago

Attachment: 25679.diff added

comment:1 by Hasan Ramezani, 9 years ago

Patch needs improvement: set

The reason is these lines of code in class ModelFormMetaclass in django/forms/models.py file:

            # Override default model fields with any custom declared ones
            # (plus, include all the other declared fields).
            fields.update(new_class.declared_fields)

I Attached a patch and also i create a pull request to fix this bug.

comment:2 by Tim Graham, 9 years ago

Resolution: invalid
Status: newclosed

As far as I understand, this is by design. Please see the documentation added in #8620 for the rationale and open a discussion on the DevelopersMailingList if you disagree with the current decisions. Thanks!

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