views:

49

answers:

1

I have a snippet of code like this in a Django application

def update_account(user_account,add_widget_count):
    user_account.add_widgets(add_widget_count)
    user_account.save()

    notify_other_system_about_account_update(user_account)

When I run this as part of my application, everything works great. In particular, when my other system (external to django) get the notification, it checks the database, and the widget count is updated.

However, when I run the above code from the admin website, there is an issue. When my other system gets the notification, it does not see the updated value in the database. I confirm by placing a breakpoint and checking in the database and it does not get updated after the call to .save() - in the non-admin-site call the DB is updated when I check during this breakpoint. Only after I let the app run its course, does the DB get updated.

Is the django admin middleware doing some sort of delaying or deferring od DB writes?

Update

I even moved the call to notify_other_system_about_account_update(user_account) to a post_save signal handler on my UserAccount object. The signal fires, but when I get it. The database updates at some point after my signal handler returns. This behavior only happens if running from within the admin website.

+4  A: 

I found the issue. The django admin uses view-wide DB transactions. I need to manually commit the transaction before notifying the external system

Well found, I'll remember that one :)
Rob Golding