Opened 2 years ago

Closed 2 years ago

#19393 closed Bug (invalid)

Form fields ordered incorrectly when introducing addition fields

Reported by: dloewenherz Owned by: nobody
Component: Forms Version: 1.5-beta-1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have a ModelForm based on a User class with fields = ('first_name', 'last_name', 'email', 'password'). My RegistrationForm looks like this:

class RegistrationForm(forms.ModelForm):
    email = forms.CharField(help_text="Used for logging in and account notifications")
    email_confirmation = forms.CharField(label="Verify Email")
    password = forms.CharField(widget=forms.PasswordInput)
    password_confirmation = forms.CharField(label="Verify Password", widget=forms.PasswordInput)

    class Meta():
        model = User
        fields = ('first_name', 'last_name', 'email', 'password')

I would expect the fields to appear in this order:

  • first_name
  • last_name
  • email
  • email_confirmation
  • password
  • password_confirmation

But instead they appear in this order

  • first_name
  • last_name
  • email
  • password
  • email_confirmation
  • password_confirmation

Change History (2)

comment:1 Changed 2 years ago by dloewenherz

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

For the time being, I'm just specifying the keyOrder in the form's init method.

self.fields.keyOrder = ['first_name', 'last_name', 'email', 'email_confirmation', 'password', 'password_confirmation']

comment:2 Changed 2 years ago by russellm

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

Thanks for the report, but AFAICT, this is a case where Django is doing exactly the right thing, and your code fix is the right solution. You've suggested the order that you would expect the fields to appear in, but I can't see any basis on which Django could determine that preferred order. Your fields.keyOrder hack is essentially the "right" solution here -- see the definition for PasswordChangeForm for an analog in Django's own codebase.

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