推荐学习书目
› 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 · Jul 8, 2015 · 3812 views
    This topic created in 4137 days ago, the information mentioned may be changed or developed.

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

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

    Supplement 1  ·  Jul 8, 2015
    自己已经解决:

    设置可为空

    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---'))
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4348 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 05:43 · PVG 13:43 · LAX 21:43 · JFK 00:43
    ♥ Do have faith in what you're doing.