そう...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.relationshipsについてまあ.. db.Columnsあるモデルからのフィールドを持つモデルのフォームを移入しましょう?
上記のコードは、関連フィールドを事前に設定しているわけではありません。それらは表示されますが、空白のままです。私が強制的にmodel_dictonaryに関係の値を含めるかどうかにかかわらず、db.Columnフィールドだけが値で事前に設定されます(たとえば、model_dictionary ['key_from_other_object'] =関連付けられた値)。
これを達成できますか?今、私は場所の上のイベントを取得する方法を把握しようとしている
form = TheModelsForm(obj=model)
form.populate_obj(model)
return form
:私は私の最初の問題を解決し :この..
編集上の何かを見つける--Can't実際に場所のフォーム上に表示するためのモデル...
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))