#13971 closed Bug (duplicate)
Excluding a declared field in ModelForm in form’s subclass
Reported by: | Mitar | Owned by: | nobody |
---|---|---|---|
Component: | Forms | Version: | 1.2 |
Severity: | Normal | Keywords: | |
Cc: | mmitar@…, cody.somerville@… | Triage Stage: | Design decision needed |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I am trying to derive (subclass) a new form from ModelForm form where I would like to remove some fields (or to have only some fields, to be more correct). Of course obvious way would be to do (base form is from django.contrib.auth.forms, this is just an example):
class MyUserChangeForm(UserChangeForm): class Meta(UserChangeForm.Meta): fields = ('first_name', 'last_name', 'email')
But this does not work as it adds/keeps also the username field in the resulting form. This field was declared explicitly in UserChangeForm. Even adding username to exclude attribute does not help.
It seems this is a bug.
The workaround is to remove the field in form constructor from self.fields
.
Attachments (2)
Change History (13)
comment:1 by , 14 years ago
comment:2 by , 14 years ago
No, I do not have a solution, I have just a workaround.
Workaround is (for example for form above) to do:
class MyUserChangeForm(UserChangeForm): class Meta(UserChangeForm.Meta): fields = ('first_name', 'last_name', 'email') def __init__(self, *args, **kwargs): super(MyUserChangeForm, self).__init__(*args, **kwargs) del self.fields['username'] # This is a declared field we really want to be removed
comment:3 by , 14 years ago
Owner: | changed from | to
---|
comment:4 by , 14 years ago
Owner: | removed |
---|---|
Triage Stage: | Unreviewed → Design decision needed |
Right now, Form class (or form super class) declared fields are always included in the form, ignoring the Meta 'exclude' and/or 'fields' attributes. To solve the issue reported here, a possible approach would be to consider filtering out the model fields not included (or excluded) in Meta from the form declared fields (or should it be in the form super class declared fields?). However, this alternative conflicts with the fix for #10363 that assumes the declared form field 'name' is available in the form although it is excluded in the form Meta definition.
I'm attaching a patch with tests that reproduce the issue and another patch that filters out excluded fields from declared fields (but make the doctest in #10363 fail). I think a design decision is needed here.
by , 14 years ago
Attachment: | 13971_patch.diff added |
---|
Possible patch that filters out not included (or excluded) fields from declared fields
comment:5 by , 14 years ago
Owner: | set to |
---|
comment:7 by , 14 years ago
Severity: | → Normal |
---|---|
Type: | → Bug |
comment:8 by , 14 years ago
Easy pickings: | unset |
---|---|
UI/UX: | unset |
Editing the user change form is something that's really common in Django projects, and as a new Django user, this was one of the first bugs I ran into. I spent a lot of time wondering if we were doing something wrong until I finally found this bug report. It would be ideal if this were fixed in the next version so as not to put off new users.
comment:9 by , 13 years ago
This bug is highly annoying and completely goes against the rest of the framework. Mitar's fix also doesnt do anything for me.
comment:10 by , 13 years ago
Resolution: | → duplicate |
---|---|
Status: | new → closed |
Closing this as a duplicate of ticket:8620
comment:11 by , 13 years ago
Cc: | added |
---|
mitar are you saying you have a solution? I don't quite understand it. Do you have a patch?
I'm also having this problem.