Code

#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

Attachments (0)

Change History (2)

comment:1 Changed 17 months 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 17 months 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.