简体   繁体   English

wtforms-alchemy-如何用关系数据预填充表单?

[英]wtforms-alchemy - what about pre-populating the form with relationship data?

so... 所以...

This: 这个:

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

This let's me pre populate the model's form with fields from the model that are db.Columns.. Well.. what about db.relationships on the model? 这让我用来自模型的db.Columns ..字段预填充模型的表单。那么,模型上的db.relationships呢?

The above code is not pre-populating the relationship fields; 上面的代码没有预填充关系字段; they show up but are left blank. 他们出现但留空。 Only the db.Column fields are pre-populated with values regardless of whether I force include the relationship values in the model_dictonary or not (for example, model_dictionary['key_from_other_object'] = associated value). 无论我是否强制在model_dictonary中包括关系值(例如,model_dictionary ['key_from_other_object'] =关联值),只有db.Column字段都预先填充了值。

Can this be accomplished? 能做到吗? --Can't find anything on this.. -找不到任何东西。

Edit: I solved my initial problem with: 编辑:我解决了我的最初的问题:

form = TheModelsForm(obj=model)
form.populate_obj(model)
return form

Now I'm trying to figure out how to get the events on the location's model to actually show up on the Location 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'))

I'm sure there is a logical reason why all the stuff I commented out isn't working.. I'm getting 'NoneType' object is not iterable 我确定有逻辑原因为什么我注释掉的所有内容都无法正常工作。我正在获取“ 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))

was barking up the wrong tree using SelectMultipleField and SelectField.. 使用SelectMultipleField和SelectField吠叫了错误的树。

Instead, 代替,

wtforms_alchemy.fields.QuerySelectMultipleField @ https://media.readthedocs.org/pdf/wtforms-alchemy/latest/wtforms-alchemy.pdf wtforms_alchemy.fields.QuerySelectMultipleField @ https://media.readthedocs.org/pdf/wtforms-alchemy/latest/wtforms-alchemy.pdf

and

this post, which helped to show usage, @ WTForms QuerySelectMultipleField Not sending List 这篇文章,有助于显示用法,@ WTForms QuerySelectMultipleField不发送列表

solved my problem.. 解决了我的问题

..ended up with ..结束于

class LocationForm(ModelForm):
    class Meta:
        model = models.Location

    events = wtforms_alchemy.fields.QuerySelectMultipleField('Event', 
        query_factory=lambda: models.Event.query.order_by(models.Event.name).all(), 
        get_label= lambda x: x.name,
        option_widget=widgets.CheckboxInput(),
        widget=widgets.ListWidget(prefix_label=False)
        )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM