Joost Barendregt
Joost Barendregt

Reputation: 406

Trying to edit an entry, but DB does not update

I'm trying to update a record in my db. The form renders well and receives the input from the views.py. After submission the redirect works, but the database is not updated with the form data. What am I missing?

model.py

class Party(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    party_name = db.Column(db.String(64))

views.py

@main.route('/edit-party/<int:id>', methods=['GET', 'POST'])
def edit_party(id):
    party = Party.query.filter_by(id=id).first_or_404()
    form = PartyForm()
    form.party_name.data = party.party_name
    if form.validate_on_submit():
        party.party_name = form.party_name.data
        db.session.add(party)
        return redirect('parties')
    return render_template('edit.html', form=form)

Upvotes: 0

Views: 35

Answers (1)

Menno H&#246;lscher
Menno H&#246;lscher

Reputation: 585

You are wiping out your updates before updating. It actually updates the DB, but form.party_name.data = party.party_name has overwritten your new data.

Upvotes: 1

Related Questions