V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
luzjoy
V2EX  ›  Python

Flask WTF 的 SelectField 如何不验证,或者说提交表单的时候可以为空

  •  
  •   luzjoy ·
    luzjoy · 2015-07-08 11:43:55 +08:00 · 3012 次点击
    这是一个创建于 3216 天前的主题,其中的信息可能已经有所发展或是发生改变。

    还有就是绑定了 列表 以后 如果多出来一个 空的选项

    class UserDetails(Form):
    group_id = SelectField(u'Group', coerce=int)

    def edit_user(request, id):
    user = User.query.get(id)
    form = UserDetails(request.POST, obj=user)
    form.group_id.choices = [(g.id, g.name) for g in Group.query.order_by('name')]

    第 1 条附言  ·  2015-07-08 13:34:49 +08:00
    自己已经解决:

    设置可为空

    parent = SelectField('Parent',coerce=int,validators=(validators.Optional(),),default=0)

    绑定完值以后增加一个为空的选项

    def __init__(self,*args,**kwargs):
    self.parent.choices = [(at.id,at.name) for at in ArticleType.query.all()]
    self.parent.choices.insert(0,(0,'---top---'))
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1071 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 23:10 · PVG 07:10 · LAX 16:10 · JFK 19:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.