问题
I have a UserForm class:
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()])
and want to make the password field Optional in UpdateUserForm:
class UpdateUserForm(UserForm):
password = TextField(u'Password', [validators.Optional()], widget=PasswordInput())
But the password field is placed after the email field, not before.
How do I preserve field order when subclassing?
Additionally, when I try to change the password field validators it doesn't work - password still Required :/ Why?
class UpdateUserForm(UserForm):
def __init__(self, **kwargs):
self.password.validators = [validators.Optional()]
super(UpdateUserForm, self).__init__(**kwargs)
or
class UpdateUserForm(UserForm):
def __init__(self, **kwargs):
self.password = TextField(u'Password', [validators.Optional()], widget=PasswordInput())
super(UpdateUserForm, self).__init__(**kwargs)
Some thoughts...
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)
Finally, what I need:
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
回答1:
In regards to your first question about reording the fields when iterating over the form object, this is what I did:
class BaseForm(Form):
def __iter__(self):
field_order = getattr(self, 'field_order', None)
if field_order:
temp_fields = []
for name in field_order:
if name == '*':
temp_fields.extend([f for f in self._unbound_fields if f[0] not in field_order])
else:
temp_fields.append([f for f in self._unbound_fields if f[0] == name][0])
self._unbound_fields = temp_fields
return super(BaseForm, self).__iter__()
class BaseUserForm(BaseForm):
password = PasswordField('Password', [Required()])
full_name = TextField('Full name', [Required()])
class NewUserForm(BaseUserForm):
username = Textfield('Username', [Required()])
field_order = ('username', '*')
That way, when you render NewUserForm (perhaps from a template which iterates over the form rendering field by field), you'll see username
, password
, full_name
. Normally you'd see username
last.
回答2:
I solved this by defining an additional __order
attribute on my Form
class, and overriding the __iter__
method so that the returned iterator's data is sorted first according to the definition. It might not be quite efficient, but there are not that many fields on a form, that it could cause any problem. It also works with fields from subclassed forms.
class MyForm(Form):
field3 = TextField()
field1 = TextField()
field2 = TextField()
__order = ('field1', 'field2', 'field3')
def __iter__(self):
fields = list(super(MyForm, self).__iter__())
get_field = lambda field_id: next((fld for fld in fields
if fld.id == field_id))
return (get_field(field_id) for field_id in self.__order)
回答3:
This is how I accomplish what were you trying to do:
class UserForm(wtforms.Form):
def __init__(self, *args, **kwargs):
super(UserForm,self).__init__(*args, **kwargs)
if kwargs.get('update', None):
self['passwd'].validators.append(wtforms.validators.Optional())
self['passwd'].flags.required = False
else:
self['passwd'].validators.append(wtforms.validators.Required())
passwd = UnicodeField(
u'Password',
[
wtforms.validators.length(max=50),
wtforms.validators.EqualTo(
'confirm',
message='Passwords must match'
)
],
widget = wtforms.widgets.PasswordInput()
)
confirm = wtforms.PasswordField(u'Password Verify')
Then, when I instantiate the UserForm, I pass update=True when editing. This appears to work for me.
回答4:
This happens because the fields ordering is defined by UnboundField.creation_counter class, which uses the order the Field class appears in the code.
>>> x1 = UserForm()
>>> x2 = UpdateUserForm()
>>> [(f[0], f[1].creation_counter) for f in x1._unbound_fields]
[('first_name', 22), ('last_name', 23), ('middle_name', 24), ('username', 25), ('password', 26), ('email', 27)]
>>> [(f[0], f[1].creation_counter) for f in x2._unbound_fields]
[('first_name', 22), ('last_name', 23), ('middle_name', 24), ('username', 25), ('email', 27), ('password', 28)]
>>>
As this is hard to solve (because wtforms try to be magic using this approach), the best way to deal with this is to define the fields in the desired order.
class BaseForm(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()])
class UserForm(BaseForm):
password = TextField(u'Password', [validators.Required()], widget=PasswordInput())
email = TextField(u'Email', [validators.Optional(), validators.Email()])
class UpdateUserForm(BaseForm):
password = TextField(u'Password', [validators.Optional()], widget=PasswordInput())
email = TextField(u'Email', [validators.Optional(), validators.Email()])
But if you are perfectionist or need to adhere to the DRY principle:
class BaseForm(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()])
class UserForm(BaseForm):
password = TextField(u'Password', [validators.Required()], widget=PasswordInput())
class UpdateUserForm(BaseForm):
password = TextField(u'Password', [validators.Optional()], widget=PasswordInput())
BaseForm.email = TextField(u'Email', [validators.Optional(), validators.Email()])
回答5:
To force an ordering on the form's fields you may use the following method:
from collections import OrderedDict
def order_fields(fields, order):
return OrderedDict((k,fields[k]) for k in order)
And call it within your forms constructor as follows:
class FancyForm(Form, ParentClass1, ParentClass2...):
x = TextField()
y = TextField()
z = TextField()
_order = 'x y z'.split()
def __init__(self, *args, **kwargs):
super(FancyForm, self).__init__(*args, **kwargs)
self._fields = order_fields(self._fields,
self._order + ParentClass1._order + ParentClass2._order)
回答6:
I have combined two answers into following snippet:
def __iter__(self):
ordered_fields = collections.OrderedDict()
for name in getattr(self, 'field_order', []):
ordered_fields[name] = self._fields.pop(name)
ordered_fields.update(self._fields)
self._fields = ordered_fields
return super(BaseForm, self).__iter__()
It's iter on BaseForm that each of my form is child of. Basically everything that is defined in field_order goes in that order, rest of the fields are rendered as-is.
来源:https://stackoverflow.com/questions/5848252/wtforms-form-class-subclassing-and-field-ordering