sqlalchemy / forcing commit

113 views
Skip to first unread message

pkraus

unread,
Feb 3, 2012, 10:29:24 PM2/3/12
to turbo...@googlegroups.com
I have a model setup to represent a customer.
I have a method on that model that returns a representation of that customer as a dictionary.
In my controller i convert that dictionary to json and feed it to a page via json.

On my page i have it setup so that when a field is updated I update that field using ajax.

In the controller i get my customer, set the new value an the field and then I want to return from the ajax call a new version of the customer as json.

The problem is that the changes haven't been committed to the database and even thought the field has been changed when i call Customer.as_dict and send it back as json its the old values.

Not a big deal i have worked around this by doing the update and then doing a request after the update for the json. However it would be nice and "cleaner" to just pass the value back dodging the second request. How can I force the commit? DBSession.flush() doesn't work. transaction.commit() breaks just about everything. For instance if you are using request.identity['user'] in a template you will get errors after the transaction commit.

How can I force the update?

I could just change the dictionary value to the new value before sending it back but the method that handles these updates in the controller is pretty generic so i would have to add a bunch of code to get the fields set correctly if i manually updated it before the return.

kvaml

unread,
Feb 4, 2012, 10:47:59 AM2/4/12
to TurboGears


On Feb 3, 10:29 pm, pkraus <paul.kr...@gmail.com> wrote:
> I have a model setup to represent a customer.
> I have a method on that model that returns a representation of that
> customer as a dictionary.
> In my controller i convert that dictionary to json and feed it to a page
> via json.
>
> On my page i have it setup so that when a field is updated I update that
> field using ajax.
>
> In the controller i get my customer, set the new value an the field and
> then I want to return from the ajax call a new version of the customer as
> json.
>
> The problem is that the changes haven't been committed to the database and
> even thought the field has been changed when i call Customer.as_dict

That looks like a @classmethod. Wouldn't customer.as_dict (a normal
method with self, not cls) do the right thing? Don't you already have
a customer object with the correct values? I realize you'd need to
fiddle the names to add a second as_dict method.

If you're retrieving a Customer object through the SQLAlchemy session
using the primary key, I'd expect SQLAlchemy to return the modified
object. You might check that also.

(Does this make sense or did I totally misunderstand what you're
doing.)
Reply all
Reply to author
Forward
0 new messages