如何通过 DjangoForms 使用实体组和祖先
我想重写 GAE djangoforms 文章中的示例以使其最显眼使用高复制数据存储在 Google App Engine 上提交表单(例如更新或添加新条目时)后的最新日期。
本文中的主要重复查询是:
query = db.GqlQuery("SELECT * FROM Item ORDER BY name")
我们将其翻译为:
query = Item.all().order('name') // datastore request
此查询我想在提交表单后从高复制数据存储中获取最新更新的数据(仅在这些情况下,我假设我可以重定向到特定的网址)提交后仅使用最新数据的查询,在所有其他情况下我不会这样做)。
验证存储结果的表单的情况如下:
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
从数据存储中获取最新条目以填充表单(用于编辑)的情况如下:
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)
那么如何将实体组/祖先键添加到这些数据存储查询中以反映表单提交后的最新数据。请注意,我不希望所有查询在填充表单(用于编辑)时和提交表单后都具有最新数据。
谁能帮我提供实际的代码示例?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果它在同一个块中,则您可以引用当前实例。
然后,一旦你
put()
它,你就可以通过以下方式获取它的id
:If it is in the same block, you have reference of the current intance.
Then once you
put()
it, you can get itsid
by: