Opened 10 years ago
Closed 10 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 |
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)
Attachments (1)
Change History (3)
by , 10 years ago
| Attachment: | 25679.diff added |
|---|
comment:1 by , 10 years ago
| Patch needs improvement: | set |
|---|
comment:2 by , 10 years ago
| Resolution: | → invalid |
|---|---|
| Status: | new → closed |
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!
The reason is these lines of code in
class ModelFormMetaclassindjango/forms/models.pyfile:I Attached a patch and also i create a pull request to fix this bug.