0

I would like to rewrite the example from the GAE djangoforms article to be show most up to date after submitting a form (e.g. when updating or adding a new entry) on Google App Engine using the High Replication Datastore.

The main recurring query in this article is:

query = db.GqlQuery("SELECT * FROM Item ORDER BY name")

which we will translate to:

query = Item.all().order('name')  // datastore request

This query I would like to get the latest updated data from the high replication datastore after submitting the form (only in these occasions, I assume I can redirect to a specific urls after submission which just uses the query for the latest data and in all other cases I would not do this).

validating the form storing the results happens like:

data = ItemForm(data=self.request.POST)
if data.is_valid():
    # Save the data, and redirect to the view page
    entity = data.save(commit=False)
    entity.added_by = users.get_current_user()
    entity.put()  // datastore request

and getting the latest entry from the datastore for populating a form (for editing) happens like:

id = int(self.request.get('id'))
item = Item.get(db.Key.from_path('Item', id))  // datastore request
data = ItemForm(data=self.request.POST, instance=item)

So how do I add entity groups/ancestor keys to these datastore queries to reflect the latest data after form submission. Please note, I don't want all queries to have the latest data, when populating a form (for editing) and after submitting a form.

Who can help me with practical code examples?

reallife
  • 329
  • 4
  • 12
  • duplicate of [How to use High Replication Datastore](http://stackoverflow.com/questions/8551605/how-to-use-high-replication-datastore) – Daniel Roseman Dec 23 '11 at 09:55
  • @DanielRoseman Please note that this is not a duplicate question (similar, yes but not duplicate), as you can see in the answers of the other question. – reallife Dec 29 '11 at 09:12
  • @moguzalp yes, I would like to display the last edited/inserted data after submitting the form. And in addition, when inserting/editing data and reloading the form, I want to make sure I have the latest version of this data prepopulated. – reallife Dec 29 '11 at 09:14

1 Answers1

0

If it is in the same block, you have reference of the current intance.
Then once you put() it, you can get its id by:

if data.is_valid():
    entity = data.save(commit=False)
    entity.added_by = users.get_current_user()
    entity.put()  
    id= entity.key().id() #this gives you inserted data id
asdf_enel_hak
  • 7,474
  • 5
  • 42
  • 84