WTForm-QuerySelectField不从数据库获取给定/默认值。烧瓶-WTForms-SQLalchemy

时间:2019-10-25 06:44:52

标签: python flask flask-sqlalchemy flask-wtforms

我使用wtform来添加和编辑对象,并使用queryselectfield作为外键。 queryselectfield始终采用行中的第一个值。据我所知,实际上已经正确传递了正确的值,只是出于某种原因而没有采用。

模型,例如:

class State(db.Model):
    __tablename__ = 'state'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    country_id = db.Column(db.Integer, db.ForeignKey('country.id'))
    name = db.Column(db.String(150))
    factor = db.Column(db.Float)
    time_created = db.Column(DateTime(timezone=True),
                             server_default=func.now())
    time_updated = db.Column(DateTime(timezone=True), onupdate=func.now())

    def __init__(self, country_id, name, factor):
        self.country_id = country_id
        self.name = name
        self.factor = factor

    def __repr__(self):
        return '{} - {}'.format(self.country_id, self.name)

示范国

class Country(db.Model):
    __tablename__ = 'country'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(150))
    shortcut = db.Column(db.String(3))
    states = db.relationship('State', backref='country', lazy=True)
    time_created = db.Column(DateTime(timezone=True),
                             server_default=func.now())
    time_updated = db.Column(DateTime(timezone=True), onupdate=func.now())

    def __init__(self, name, shortcut):
        self.name = name
        self.shortcut = shortcut

    def __repr__(self):
        return '{} - {} - {}'.format(self.id, self.name, self.shortcut)

查看:

@admin_blueprint.route('/admin/state/edit/<int:state_id>',
                       methods=['GET', 'POST'])
@login_required
def edit_state(state_id):
    """Edit state"""
    obj = State.query.filter_by(id=state_id).first_or_404()
    form = StateForm(obj=obj)
    if request.method == 'POST' and form.validate_on_submit:
        obj.country_id = form.country_id.data.id
        obj.name = form.name.data
        obj.factor = form.factor.data
        db.session.commit()
        log_entry_edit(State, obj)
        return redirect(url_for('admin.list_states'))
    return render_template('admin/state_form.html', form=form,
                           title='Edit State')

表格:

def countries():
    return Country.query

class StateForm(FlaskForm):
    name = StringField('State Name')
    factor = FloatField('Factor')
    country_id = QuerySelectField('Country', query_factory=countries)
    submit = SubmitField('Save')

模板:

    <div>
         {{ form.country_id.label }} 
         {{ form.country_id(class="form-control form-group") }}
         <output style="color: red;"  for="country_id" id="selected-country_id">
              ACTUAL ID: {{ form.country_id.data }}
         </output>
    </div>
    {% if form.country_id.errors %}
        <ul class="errors">
            {% for error in country_id.errors %}
                <li>{{ error }}</li>
            {% endfor %}
        </ul>
    {% endif %}

如您在下图中看到的那样,该表单采用第一个值,并且如果我不再次选择我想首先使用的值,它将覆盖db。 实际的正确值会到达表格(红色ID):

screenshot of template output

我刚开始使用烧瓶,wtforms等,老实说我看不到我在做什么错。有人有线索吗?

1 个答案:

答案 0 :(得分:1)

我遇到了一个类似的问题,最后弄清楚了:在使用“ POST”进行设置之前,您必须在调用表单('GET')时分别获取外键的数据:

if request.method == 'GET':
    form.country_id.data = obj.country_id
if request.method == 'POST' and form.validate_on_submit:
    # as before