【问题标题】:Is it possible to limit choices in SelectModelField in wtf-peewee是否可以在 wtf-peewee 中限制 SelectModelField 中的选择
【发布时间】:2012-03-09 02:47:10
【问题描述】:

我将烧瓶与 flask-peewee 和 wtfpeewee 一起使用。
所以,我有这样的模型:

class Category(Model):
    name = CharField()
    user = ForeignKeyField(User, null=True)

class Record(Model):
    value = DecimalField()
    category = ForeignKeyField(Category)
    user = ForeignKeyField(User)

当我为用户创建表单以添加记录时,我是这样做的:

RecordForm = model_form(Record)

数据库中的所有类别都可以在此表单的“类别”字段中选择,但我需要将“类别”字段的可用选项限制为用户字段等于无或当前(登录)用户的类别子集.我知道如何通过查询来限制它,但是对于表单字段应该怎么做呢?

【问题讨论】:

    标签: python flask wtforms peewee


    【解决方案1】:

    抱歉刚刚看到这个

    您可以在类定义时执行此操作:

    from wtfpeewee.fields import SelectQueryField
    
    class MyForm(Form):
        category = SelectQueryField(query=Category.filter(some_val=other_val)
    

    或者,我相信您可以在运行时执行此操作:

    my_form = MyForm()
    my_form.category.query = Category.filter(user=some_user)
    

    【讨论】:

      猜你喜欢
      • 2011-01-14
      • 1970-01-01
      • 1970-01-01
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-08
      相关资源
      最近更新 更多