Reputation: 20875
I am running Django on Google App Engine. I made a simple application that resembles a bookstore. After I submit a form that edits the title of a book, my app redirects me to a listing of all books.
I noticed that after I edit a book's title, the book listing that I am redirected to does not immediately reflect my update. The update is reflected in the listing after I refresh the page. I suspect that this is due to the data store obtaining the version of my Book entity prior to the update transaction to maintain strong consistency.
How do I ensure that the user is redirected to a book listing with the updated book title after the form is submitted? I could just return an HTTP response solely based on the updated Book object in my app (and thus forgo fetching for the book from the data store), but then, the URL won't change from that of the form page.
Upvotes: 1
Views: 102
Reputation: 57168
Put your entities into an entity group for consistency; if you make an ancestor query, you're guaranteed consistent results. The catch is that it limits the number of writes in that group to around 5 per second. You could have an entity group per "shop" or something, perhaps.
Another option is: the entity data is immediately updated; it's just the index that's wrong, so if you're showing a list of books, you can always query for book keys (only) and then get those books by key and show that data, rather than querying for the books out of the index directly. Of course, in this case, the sort and filters may disagree with the actual displayed information, so you have to either fix that up yourself or tolerate some weirdness.
Upvotes: 1
Reputation: 10360
It can't handle all situations, but passing the key of the newly created/edited entity along with your request will allow you go do a get on that entity, and replace the outdated version from the query with the get'd version, which will be current.
(Be aware that in the SDK, merely doing the get is enough to ensure that subsequent queries will return the current data - this is not how it works in the real datastore.)
Upvotes: 1
Reputation: 41089
A very inelegant solution is to introduce a delay to give App Engine time to commit the writes.
A better approach may be to store a book object in Memcache. Before you update it in the datastore, update it in Memcache. The next request will find it in Memcache, so it will not have to fetch it from the datastore.
Upvotes: 1