I use flask with flask-peewee and wtfpeewee.
So, I have models like that:
class Category(Model):
name = CharField()
user = ForeignKeyField(User, null=True)
class Record(Model):
value = DecimalField()
category = ForeignKeyField(Category)
user = ForeignKeyField(User)
When I create form for user to add Record, I do it that way:
RecordForm = model_form(Record)
All categories in database are available for choice in field 'Category' of this form, but I need to limit available choices for 'Category' field to a subset of categories that have user field equal None or current (logged in) user. I know how to limit it by query, but how should that be done for a form field?
Sorry to see this just now
You can do this at class definition time:
from wtfpeewee.fields import SelectQueryField
class MyForm(Form):
category = SelectQueryField(query=Category.filter(some_val=other_val)
Alternatively, I believe you can do this at runtime:
my_form = MyForm()
my_form.category.query = Category.filter(user=some_user)