繁体   English   中英

如何在FormField中使用Flask / WTForms中的populate_obj?

[英]How to use populate_obj in Flask / WTForms with a FormField?

我有一个位置用户。 正如概念证明一样,Location是CombinedForm中的FormField,应该存储为用户模型。 最终我想拥有相当多的嵌套表单,所以我真的很喜欢form.populate_obj(Model)来处理数据。 但是,我一定是做错了。 这是我的代码:

# - - - Models - - -
class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer(), primary_key=True)
    username = db.Column(db.String(40))
    location = db.relationship('Location', backref='user')

class Location(db.Model):
    __tablename__ = 'locations'
    id = db.Column(db.Integer(), primary_key=True)
    user_id = db.Column(db.Integer(), db.ForeignKey('users.id'))
    descr = db.Column(db.String(50))

# - - - Forms - - -
class LocationForm(NoCsrfForm):
    descr = StringField('Location Name')

class CombinedForm(Form):
    username = StringField('User', validators=[DataRequired()])
    location = FormField(LocationForm)  # , default=lambda: Location())
    submit = SubmitField('Submit')

# - - - Routes - - -
@app.route('/', methods=['GET', 'POST'])
def index():
    user = User(username="not in db")
    form = CombinedForm(obj=user)
    if form.validate_on_submit():
        form.populate_obj(user)
        db.session.add(user)
        db.session.commit()
    return render_template('multi.html', form=form)

当我将user.location = [Location(descr="Test")]到索引函数时,我可以在我的视图中使用{{ form.location }}渲染字段,但是对表单中字段的更改没有对模型的影响,因为populate_obj不会使用POST数据填充Location对象。 当FormField在FieldList中时,填充它是有效的。

我错过了什么?

我没有找到没有FieldList的工作FormField示例。

我花了很多时间在这上面,甚至在我认为我已经弄明白的时候做了一个例子 ,但我错了,至少在没有FieldList的情况下使用带有populate_list的FormField时。 如果有更好的方法来处理一个表格中的2-3个模型的数据,请告诉我。 我疯了所以我真的很感激一些帮助。 谢谢你的时间。

看起来我的头脑里真的混淆了,关系错了。 以下示例有效。

请注意,如果您确实将对象(如User实例)传递给表单,则必须已填充该位置。 如果您传递的用户没有位置,则populate_obj无法在提交时从表单中找到新位置。

有办法吗?

这应该作为FormField概念证明:

# - - - Models - - -
class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer(), primary_key=True)
    username = db.Column(db.String(40))
    location_id = db.Column(db.Integer, db.ForeignKey('locations.id'))


class Location(db.Model):
    __tablename__ = 'locations'
    id = db.Column(db.Integer(), primary_key=True)
    descr = db.Column(db.String(50))
    users = db.relationship('User', backref='location')


# - - - Forms - - -
class LocationForm(NoCsrfForm):
    descr = StringField('Location Name')


class CombinedForm(Form):
    username = StringField('User', validators=[DataRequired()])
    location = FormField(LocationForm, default=lambda: Location())
    submit = SubmitField('Submit')


# - - - Routes - - -
@app.route('/', methods=['GET', 'POST'])
def index():
    user = User(username="Def")
    form = CombinedForm()  # don't put a user obj in unless it has a location!
    if form.validate_on_submit():
        form.populate_obj(user)
        db.session.add(user)
        db.session.commit()
    return render_template('multi.html', form=form)

暂无
暂无

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

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