所以...wtforms-alchemy - 如何預先填充關係數據的表單?
此:
model_dictionary = model.as_dict() # some kind of dictionary
form = TheModelsForm(**model_dictionary) # https://wtforms-alchemy.readthedocs.io/en/latest/advanced.html
form.populate_obj(model)
return form
這讓我的預填充模型的形式與那些db.Columns模型領域..嗯..怎麼樣型號db.relationships ?
上面的代碼不預先填充關係字段;他們出現,但留空。無論我是否在model_dictonary中包含關係值(例如,model_dictionary ['key_from_other_object'] = associated value),只有db.Column字段預先填充了值。
這可以完成嗎? --Can't找到這樣的東西..
編輯: 我解決了我最初的問題:
form = TheModelsForm(obj=model)
form.populate_obj(model)
return form
現在,我試圖找出如何獲得該位置的事件模型實際顯示的位置表格上...
class Event(Base):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String())
location_id = db.Column(db.Integer, db.ForeignKey('location.id'))
class Location(Base):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String())
events = db.relationship('Event', backref=db.backref('events'))
我敢肯定有就是爲什麼所有我註釋掉的東西是不工作..我得到「NoneType」對象是一個合乎邏輯的理由不可迭代
class EventForm(ModelForm):
class Meta:
model = models.Event
location_id = fields.SelectField(u'Location', coerce=int)
class LocationForm(ModelForm):
class Meta:
model = models.Location
#events = fields.SelectField(u'Event', choices=[(g.id, g.selector) for g in models.Event.query.all()], coerce=int)
# events = fields.SelectMultipleField('Event',
# choices=[(g.id, g.selector) for g in models.Event.query.all()],
# coerce=int,
# option_widget=widgets.CheckboxInput(),
# widget=widgets.ListWidget(prefix_label=False)
# )
#events = ModelFieldList(fields.FormField(EventForm))