Note that there are some explanatory texts on larger screens.

plurals
  1. POwtforms Form class subclassing and field ordering
    text
    copied!<p>I have a UserForm class:</p> <pre><code>class UserForm(Form): first_name = TextField(u'First name', [validators.Required()]) last_name = TextField(u'Last name', [validators.Required()]) middle_name = TextField(u'Middle name', [validators.Required()]) username = TextField(u'Username', [validators.Required()]) password = TextField(u'Password', [validators.Required()], widget=PasswordInput()) email = TextField(u'Email', [validators.Optional(), validators.Email()]) </code></pre> <p>and want to make the password field Optional in UpdateUserForm:</p> <pre><code>class UpdateUserForm(UserForm): password = TextField(u'Password', [validators.Optional()], widget=PasswordInput()) </code></pre> <p>But the password field is placed after the email field, not before.</p> <p>How do I preserve field order when subclassing?</p> <p>Additionally, when I try to change the password field validators it doesn't work - password still Required :/ Why?</p> <pre><code>class UpdateUserForm(UserForm): def __init__(self, **kwargs): self.password.validators = [validators.Optional()] super(UpdateUserForm, self).__init__(**kwargs) </code></pre> <p>or</p> <pre><code>class UpdateUserForm(UserForm): def __init__(self, **kwargs): self.password = TextField(u'Password', [validators.Optional()], widget=PasswordInput()) super(UpdateUserForm, self).__init__(**kwargs) </code></pre> <h3>Some thoughts...</h3> <pre><code>class UpdateUserForm(UserForm): def __init__(self, formdata=None, obj=None, prefix='', **kwargs): self._unbound_fields[4][1] = TextField(u'Password', [validators.Optional()], widget=PasswordInput()) UserForm.__init__(self, formdata=None, obj=None, prefix='', **kwargs) </code></pre> <p>Finally, what I need:</p> <pre><code>class UpdateUserForm(UserForm): def __init__(self, formdata=None, obj=None, prefix='', **kwargs): UserForm.__init__(self, formdata, obj, prefix, **kwargs) self['password'].validators = [validators.Optional()] self['password'].flags.required = False </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload