简体   繁体   English

如何从Google App Engine NDB更新查询?

[英]How do I update a query from Google App Engine NDB?

We are using Google App Engine in Python. 我们正在Python中使用Google App Engine。 I have code that saves a new object to the database, and then queries the database to receive all the objects. 我有将新对象保存到数据库,然后查询数据库以接收所有对象的代码。 The problem is that the query returns all the objects except the new object I created. 问题是查询返回除我创建的新对象以外的所有对象。 Only after refreshing the page I see the new object. 只有刷新页面后,我才能看到新对象。 Is there a way to update the query to include all the objects, including the new object I created? 有没有一种方法可以更新查询以包括所有对象,包括我创建的新对象? Here is my code: 这是我的代码:

if (self.request.get("add_a_new_feature") == "true"):
    features = Feature.gql("WHERE feature_name=:1 ORDER BY last_modified DESC LIMIT 1", NEW_FEATURE_NAME) # class Feature inherits from ndb.Model
    if (features.count() == 0):
        new_feature = Feature(feature_name=NEW_FEATURE_NAME)
        new_feature.put()
...
features = Feature.gql("ORDER BY date_created")
if (features.count() > 0):
    features_list = features.fetch()
    for feature in features_list:
        ... # the list doesn't contain new_feature

As mentioned in the comments - this is an expected behaviour. 如评论中所述-这是预期的行为。 Take a look at this article for additional information. 请参阅本文以获取更多信息。 As a quick fix/hack you could simply get the data from datastore before adding the new entity and then append it to the list. 作为快速修复/黑客,您可以在添加新实体之前简单地从数据存储中获取数据,然后将其追加到列表中。

features = Feature.gql("ORDER BY date_created")

if (self.request.get("add_a_new_feature") == "true"):
    if (Feature.gql("WHERE feature_name=:1 ORDER BY last_modified DESC LIMIT 1", NEW_FEATURE_NAME).count() == 0):
        new_feature = Feature(feature_name=NEW_FEATURE_NAME)
        new_feature.put()
        features.append(new_feature)
...

if (features.count() > 0):
    features_list = features.fetch()
    for feature in features_list:
        ... # the list now contain the new_feature at the end

Depending on what Entity.gql() returns when there are no results (None or [ ]?) you may need to check whether features is a list before appending. 根据没有结果(无或[]?)时返回的Entity.gql()您可能需要在附加之前检查features是否为列表。 You could also probably avoid the second query since you already have a list of features and could loop through it in Python rather than sending another request to datastore. 您还可以避免第二个查询,因为您已经有了功能列表,并且可以在Python中遍历它,而不是向数据存储发送另一个请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM