Join the Stack Overflow Community
Stack Overflow is a community of 6.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I am using the following passage of code:

@app.route('/budget_item/<int:budget_id>/edit', methods=['GET', 'POST'])
   def budget_item_edit(budget_id):


budget_item = session.query(Budget).filter_by(id=budget_id).one()
print "Start EDIT sequence"


# Return form data from HTML initial load form
elif request.method == 'POST':

  budget_amount_reallocated_total = budget_item.budget_amount_reallocated_total


  #ORIGINAL BUDGET
  if request.form['transaction_type'] == 'Original Budget':

    #amount
    if request.form['amount'] == "":
      amount = 0
    else:
      amount = float(str(request.form['amount']))

    budget_item = Budget(
      #created_date = "",
      budget_transaction_type = request.form['transaction_type'],
      budget_line = request.form['budget_line'],
      amount = amount,
      description = request.form['description']
      #date_received = request.form['date_received']
      )

    try:
      count = 1

      while count < 10000:
        count += 1

        #budget_line
        setattr(budget_item,'budget_line'+str(count),request.form['budget_line'+str(count)])

        #amount
        setattr(budget_item,'amount'+str(count),float(request.form['amount'+str(count)]))
        budget_amount_reallocated_total += float(request.form['amount'+str(count)])
        setattr(budget_item, 'budget_amount_reallocated_total', budget_amount_reallocated_total)

        #description
        setattr(budget_item,'description'+str(count), request.form['description'+str(count)])

        #date_received
        setattr(budget_item,'date_received'+str(count),request.form['date_received'+str(count)])
        session.commit()

    except:
      session.commit()
      return redirect(url_for('budget_master'))

  else:
    print "I'm done! This is not a post request"

This block of code is setup to pass data from an HTML via a POST request an then update a corresponding object in the Postgres DB. I can confirm that the object queried from the DB "budget_item" is being updated by settattr. At the end of the passage, I use commit() to update the object; however, the database doesn't reflect the changes. Just to test to make sure things are flowing, I've tried session.add(budget_item) followed by session.commit() to make sure the connect to the DB is OK. That works. How do i update this budget_item object into the database? Any help is much appreciated.

share|improve this question
1  
What are you trying to do? You seem to be adding 50000! new attributes to a new Budget instance. Note that the second budget_item is a different instance that the one that you retrieved initially. Since none of those attributes exist in the mapping (well, I assume that they don't), updating the budget_item will have no affect. Also, it's a new instance, so you need to add it to the session. Perhaps you should explain a little more what it is that you are trying to do. – mhawke Jun 8 '16 at 5:11

i think that a simple

budget_item.budget_amount_reallocated_total = budget_amount_reallocated_total
session.add(budget_item)
session.commit()

is the right way to do it

share|improve this answer
    
unfortunately, this doesn't just edit an existing object, it duplicates the object in the database... leaving the old object and adding a new one as well. – user3308477 Jun 8 '16 at 11:33

To answer your question, to update the budget_item that already exists in the database you need to update the Budget instance that you retrieved from the database, i.e.

budget_item = session.query(Budget).filter_by(id=budget_id).one()

not the one that you have newly created with:

budget_item = Budget(...)

Here the first budget_item represents the row in the database, so this is the one to update. To that end you can replace the code that creates the second Budget instance with this:

budget_item.budget_transaction_type = request.form['transaction_type']
budget_item.budget_line = request.form['budget_line']
budget_item.amount = amount
budget_item.description = request.form['description']

Once you have finished updating the Budget instance you can call session.commit() to flush it to the database.

As mentioned in my comment to your question, it appears that you are trying to add a large number of additional attributes to budget_item all of which will be ignored by sqlalchemy unless they are defined in the mapping between the Budget instance and the Budget table.

share|improve this answer
    
This is exactly correct. Thank you. The line budget_item = Budget(...) was in fact instantiating a new object and not just updated the queried object from the DB. When i revised it, the original budget_item updated correctly. Thank you! (As for the additional attributes, yes I should modify the code. My html form only allows 10 attributes as does my DB table. I set the loop at 10000 possible passes in case I came back later to add more attributes to the front end form and the db table. For now, the loop stops running and hits and exception when their are no corresponding POST request dict. – user3308477 Jun 8 '16 at 11:34

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.