Edit form creates new data instead

I defined a route and template to edit data. Instead, it creates new data. How do I fix this?

@home_blueprint.route('/table/<int:data_id>/edit', methods=['GET', 'POST'])
def edit(data_id):
    ed_form = EditTableForm(request.form)
    data_to_edit = db.session.query(Diary).filter_by(id=data_id)
    if ed_form.validate_on_submit():
        if ed_form.title.data:
            data_to_edit.title = ed_form.title.data
        if ed_form.weight.data:
            data_to_edit.weight = ed_form.weight.data
        if ed_form.kkal.data:
            data_to_edit.kkal = ed_form.kkal.data
        if ed_form.carbs.data:
            data_to_edit.carbs = ed_form.carbs.data
        if ed_form.proteins.data:
            data_to_edit.proteins = ed_form.proteins.data
        if ed_form.fats.data:
            data_to_edit.fats = ed_form.fats.data
        db.session.add(data_to_edit)
        db.session.commit()
        flash('New data was successfully posted. Thanks.')
        return redirect(url_for('home.table'))
    else:
        return render_template('edit.html', data_to_edit=data_to_edit, ed_form=ed_form, data_id=data_id)
    return render_template("edit.html", diary=diary)
  <table class="table table-bordered">
    <thead>
      <tr>
          <th>Product</th>
          <th>Weith</th>
          <th>Kkal</th>
          <th>Protein</th>
          <th>Fat</th>
          <th>Carbs</th>
      </tr>
    </thead>
    <tbody>
    {% for data in data_to_edit %}
    <tr>
        <form class="form-message" role="form" method="post" action="/table">
            {{ ed_form.csrf_token }}
            <td>{{ed_form.title(placeholder=data.title)}}</td>
            <td>{{ed_form.weight(placeholder=data.weight)}}</td>
            <td>{{ed_form.kkal(placeholder=data.kkal)}}</td>
            <td>{{ed_form.carbs(placeholder=data.carbs)}}</td>
            <td>{{ed_form.proteins(placeholder=data.proteins)}}</td>
            <td>{{ed_form.fats(placeholder=data.fats)}}</td>
            <td><button class="btn btn-sm btn-success" type="submit">Post</button></td>
        </form>
    </tr>
    {% endfor %}
    </tbody>
  </table>

Link to projects Git repository. Each users input into table has own id, I query data by id and then trying to Edit data there with no luck so far :)

UPDATE I have changed code to:

data_to_edit = db.session.query(Diary).filter_by(id=data_id).first()
if ed_form.validate_on_submit():
    if not data_to_edit:
        data_to_edit = Diary(
        ed_form.title.data,
        ed_form.weight.data,
        ed_form.kkal.data,
        ed_form.carbs.data,
        ed_form.proteins.data,
        ed_form.fats.data,
        current_user.id
        )
        db.session.add(data_to_edit)
        db.session.commit()

But yet no luck, it doesn't change old data but ads new.

Upvotes: 0

Views: 91

Answers (1)

Tasos Vogiatzoglou
Tasos Vogiatzoglou

Reputation: 2453

The issue is that you are adding the object again in session, thus inserting it into the database.

Sample code

data_to_edit = db.session.query(Diary).filter_by(Diary.id=data_id).first()
if not data_to_edit:
    data_to_edit = Diary()
    db.session.add(data_to_edit)
...
# edit properties
...
db.session.commit()

The idea is to add the new object only when it's actually new.

Upvotes: 1

Related Questions