Opened 14 years ago

Closed 12 years ago

Last modified 12 years ago

#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)

13971_tests.diff (1.4 KB ) - added by matiasb 13 years ago.
Tests added that shows the issue.
13971_patch.diff (3.0 KB ) - added by matiasb 13 years ago.
Possible patch that filters out not included (or excluded) fields from declared fields

Download all attachments as: .zip

Change History (13)

comment:1 by Jacob Fenwick, 14 years ago

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.

comment:2 by Mitar, 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 matiasb, 13 years ago

Owner: changed from nobody to matiasb

comment:4 by matiasb, 13 years ago

Owner: matiasb removed
Triage Stage: UnreviewedDesign 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 matiasb, 13 years ago

Attachment: 13971_tests.diff added

Tests added that shows the issue.

by matiasb, 13 years ago

Attachment: 13971_patch.diff added

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

comment:5 by matiasb, 13 years ago

Owner: set to nobody

comment:6 by Mitar, 13 years ago

Thanks for checking it.

comment:7 by Graham King, 13 years ago

Severity: Normal
Type: Bug

comment:8 by Lee Semel, 13 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 anonymous, 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 Cody A.W. Somerville, 12 years ago

Resolution: duplicate
Status: newclosed

Closing this as a duplicate of ticket:8620

comment:11 by Cody A.W. Somerville, 12 years ago

Cc: cody.somerville@… added
Note: See TracTickets for help on using tickets.
Back to Top