我将flask与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)数据库中的所有类别都可以在此表单的“类别”字段中选择,但我需要将“类别”字段的可用选择限制为用户字段等于“无”或“当前(已登录)用户”的类别子集。我知道如何通过查询来限制它,但是应该如何对表单域进行限制呢?
发布于 2012-03-28 00:19:33
很抱歉现在才看到这个
您可以在类定义时执行此操作:
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)https://stackoverflow.com/questions/9628271
复制相似问题