app-engine-ndb

How to read old property values in a _pre_put_hook

烂漫一生 提交于 2020-01-02 02:06:27
问题 I am trying to implement an ndb model audit so that all changes to properties are stored within each model instance. Here is the code of the _pre_put_hook I chose to implement that. def _pre_put_hook(self): # save a history record for updates if not (self.key is None or self.key.id() is None): old_object = self.key.get(use_cache=True) for attr in dir(self): if not callable(getattr(self, attr)) and not attr.startswith("_"): if getattr(self, attr) != getattr(old_object, attr): logging.debug(

ndb retrieving entity key by ID without parent

江枫思渺然 提交于 2020-01-01 09:40:09
问题 I want to get an entity key knowing entity ID and an ancestor. ID is unique within entity group defined by the ancestor. It seems to me that it's not possible using ndb interface. As I understand datastore it may be caused by the fact that this operation requires full index scan to perform. The workaround I used is to create a computed property in the model, which will contain the id part of the key. I'm able now to do an ancestor query and get the key class SomeModel(ndb.Model): ID = ndb

What's the equivalent of Entity.all(keys_only=True).fetch(20) in NDB?

♀尐吖头ヾ 提交于 2020-01-01 08:43:38
问题 How do I get the equivalent result of the following query in NDB? Entity.all(keys_only=True).fetch(20) I know you can pass 'keys_only=True' to the iter() method. But what if I want to perform a keys only fetch, how do I do that in NDB? 回答1: Found it in the GAE NDB Docs. The answer is Entity.query().fetch(20,keys_only=True) . 来源: https://stackoverflow.com/questions/10203874/whats-the-equivalent-of-entity-allkeys-only-true-fetch20-in-ndb

Many-To-Many Relationships in Google App Engine Datastore (ndb)

五迷三道 提交于 2020-01-01 05:16:48
问题 I have two models: Members and Events. A member can participe in many events and in an event, there are many participants. I think about like this: class Members(ndb.model): event = ndb.KeyProperty(repeated=True) class Events(ndb.model): member = ndb.KeyProperty(repeated=True) What's the best way to do many-to-many relationship? 回答1: I think in this case you want to have an array/list of keys in one model pointing to the other model. Since there is a limit on the length of the array/list (the

ndb modelling one-to-many: merits of repeated KeyProperty vs foreign key

蹲街弑〆低调 提交于 2019-12-31 10:41:33
问题 My question is about modelling one-to-many relations in ndb. I understand that this can be done in (at least) two different ways: with a repeated property or with a 'foreign key'. I have created a small example below. Basically we have an Article which can have an arbitrary number of Tags. Let's assume that a Tag can be removed but cannot be changed after it has been added. Let's also assume that we don't worry about transactional safety. My question is: what is the preferred way of modelling

Can't execute a distinct projection query

谁说我不能喝 提交于 2019-12-31 04:11:14
问题 I have a simple little "Observation" class: from google.appengine.ext import ndb class Observation(ndb.Model): remote_id = ndb.StringProperty() dimension_id = ndb.IntegerProperty() metric = ndb.StringProperty() timestamp_observed = ndb.StringProperty() timestamp_received = ndb.DateTimeProperty(auto_now_add=True) @classmethod def query_book(cls): return cls.query() I can run projection queries against the Datastore to return only certain columns. E.g: observations = Observation.query().fetch

Can't execute a distinct projection query

主宰稳场 提交于 2019-12-31 04:11:05
问题 I have a simple little "Observation" class: from google.appengine.ext import ndb class Observation(ndb.Model): remote_id = ndb.StringProperty() dimension_id = ndb.IntegerProperty() metric = ndb.StringProperty() timestamp_observed = ndb.StringProperty() timestamp_received = ndb.DateTimeProperty(auto_now_add=True) @classmethod def query_book(cls): return cls.query() I can run projection queries against the Datastore to return only certain columns. E.g: observations = Observation.query().fetch

How to programmatically determine if ndb property is multivalue

偶尔善良 提交于 2019-12-30 10:58:29
问题 I am translating an app from Datastore to ndb and have encountered a problem in the xml import routine. The problem is that I am not able to programmatically determine whether a property of a ndb.model class is a multivalue property or not. I suspect that this is due to lack of basic Python skills since the code I have come up with so far shows that the value is “visible”. I am thus not able to grab it. Please help. from google.appengine.ext import ndb class House(ndb.Model): name = ndb

Does ndb.toplevel break transactions?

一曲冷凌霜 提交于 2019-12-30 03:20:49
问题 The following code works as expected and does not trigger the assertion: @ndb.transactional @ndb.tasklet def Foo(): assert ndb.in_transaction() The following code breaks, triggering the assertion: @ndb.transactional @ndb.toplevel def Foo(): assert ndb.in_transaction() I tried replacing the decorator with an ndb.transaction call or an ndb.transaction_async call, but neither worked. Is there a bug with ndb.toplevel and transactions? 回答1: I discovered that the problems is that both create new

How to use AJAX with Google App Engine (Python)

 ̄綄美尐妖づ 提交于 2019-12-29 18:01:49
问题 I am completely novice at AJAX. I am familiar with HTML/CSS, jQuery and beginner at GAE and Python. In an effort to understand how AJAX works, I would like to know how AJAX might be used (actual code) in this example below. Let's use a reddit-like example where vote ups/downs are ajaxified: Here is the Story Kind: class Story(ndb.Model): title = ndb.StringProperty(required = True) vote_count = ndb.IntegerProperty(default = 0) The HTML would look like this: <h2>{{story.title}}</h2> <div> {