wtforms 表单类子类化和字段排序

发布于 2024-11-04 12:53:06 字数 1900 浏览 1 评论 0原文

我有一个 UserForm 类:

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()])

并且希望在 UpdateUserForm 中将密码字段设置为可选:

class UpdateUserForm(UserForm):
    password = TextField(u'Password', [validators.Optional()], widget=PasswordInput())

但密码字段放置在电子邮件字段之后,而不是之前。

子类化时如何保留字段顺序?

此外,当我尝试更改密码字段验证器时,它不起作用 - 仍然需要密码:/为什么?

class UpdateUserForm(UserForm):
    def __init__(self, **kwargs):
        self.password.validators = [validators.Optional()]
        super(UpdateUserForm, self).__init__(**kwargs)

class UpdateUserForm(UserForm):
    def __init__(self, **kwargs):
        self.password = TextField(u'Password', [validators.Optional()], widget=PasswordInput())
        super(UpdateUserForm, self).__init__(**kwargs)

一些想法...

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)

最后,我需要什么:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

神经暖 2024-11-11 12:53:06

关于您在迭代表单对象时重新排序字段的第一个问题,这就是我所做的:

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', '*')

这样,当您渲染 NewUserForm (可能来自逐个字段迭代表单渲染字段的模板)时,您会看到用户名密码全名。通常您会最后看到用户名

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.

银河中√捞星星 2024-11-11 12:53:06

我通过在 Form 类上定义一个附加的 __order 属性并覆盖 __iter__ 方法来解决这个问题,以便首先根据返回的迭代器的数据进行排序到定义。它可能不是很有效,但是表单上没有太多字段,因此可能会导致任何问题。它还适用于子类表单中的字段。

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)

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)
静谧 2024-11-11 12:53:06

这就是我完成您想要做的事情的方法:

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')

然后,当我实例化 UserForm 时,我在编辑时传递 update=True 。这似乎对我有用。

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.

我三岁 2024-11-11 12:53:06

要强制对表单字段进行排序,您可以使用以下方法:

from collections import OrderedDict

def order_fields(fields, order):
    return OrderedDict((k,fields[k]) for k in order)

并在表单构造函数中调用它,如下所示:

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)

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)
柏林苍穹下 2024-11-11 12:53:06

发生这种情况是因为字段排序是由 UnboundField.creation_counter 类定义的,该类使用 Field 类在代码中出现的顺序。

>>> 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)]
>>> 

由于这很难解决(因为 wtforms 试图使用这种方法变得神奇),因此处理此问题的最佳方法是按所需的顺序定义字段。

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()])

但如果您是完美主义者或需要遵守DRY 原则

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()])

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()])
草莓酥 2024-11-11 12:53:06

我将两个答案合并到以下代码片段中:

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__()

它是 BaseForm 上的 __iter__,我的每个表单都是其子表单。基本上,field_order 中定义的所有内容都按该顺序进行,其余字段按原样呈现。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文