Opened 5 years ago

Closed 3 years ago

Last modified 3 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 5 years ago.
Tests added that shows the issue.
13971_patch.diff (3.0 KB) - added by matiasb 5 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 Changed 5 years ago by jfenwick

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 Changed 5 years ago by mitar

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 Changed 5 years ago by matiasb

  • Owner changed from nobody to matiasb

comment:4 Changed 5 years ago by matiasb

  • Owner matiasb deleted
  • Triage Stage changed from Unreviewed to 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.

Changed 5 years ago by matiasb

Tests added that shows the issue.

Changed 5 years ago by matiasb

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

comment:5 Changed 5 years ago by matiasb

  • Owner set to nobody

comment:6 Changed 5 years ago by mitar

Thanks for checking it.

comment:7 Changed 4 years ago by graham_king

  • Severity set to Normal
  • Type set to Bug

comment:8 Changed 4 years ago by lsemel

  • 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 Changed 4 years ago by anonymous

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 Changed 3 years ago by codysomerville

  • Resolution set to duplicate
  • Status changed from new to closed

Closing this as a duplicate of ticket:8620

comment:11 Changed 3 years ago by codysomerville

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