app-engine-ndb

How can I get the ndb.Model when my only input is an ndb.Query?

主宰稳场 提交于 2019-11-29 16:46:32
Let's say there is ndb.Model that looks like this: class Foo(ndb.Model): bar = ndb.StringProperty() My question is, if my only input is the Foo.query() how can I get the model as an object that this query belongs to? def query_to_model(query): # some magic return model The Foo.query().kind return the model's name as a string, but I didn't manage to find a way to get it as an object. The following works using eval , but only when the model is defined in the same file: def query_to_model(query): return eval(query.kind) I want something more general than that. Greg After you have imported code

Google App Engine NDB: How to store document structure?

徘徊边缘 提交于 2019-11-29 16:10:11
问题 From App Engine NDB documentation: The NDB API provides persistent storage in a schemaless object datastore. It supports automatic caching, sophisticated queries, and atomic transactions. NDB is well-suited to storing structured data records. I want to create a structure like the following using NDB, where each instance looks like : { city: 'SFO' date: '2013-01-27' data: { 'keyword1': count1, 'keyword2': count2, 'keyword3': count3, 'keyword4': count4, 'keyword5': count5, .... } } How can I

Query random row in ndb [closed]

那年仲夏 提交于 2019-11-29 15:53:49
I am trying to develop an web app on google app engine using webapp2. One of the thing I need to do is to retrieve a random from ndb and display it. Is it any effective method that allows me to do so? I assume you mean a random record when you say "random from ndb". If you are using automatic id's you could use the following approach. (how sparse you id's will affect how successful this will be). use random.randrange(start, stop) with start being 0, stop being (2^52)-1 , given the new id allocation policy. do a keys only query for keys greater than key created from random id. if no results try

How can a multi-property ndb query be successful without a composite index?

北战南征 提交于 2019-11-29 11:38:17
I have this entity model: class ApartCILabel(ndb.Model): project_id = ndb.IntegerProperty(indexed=True) changeset_ids = ndb.IntegerProperty(repeated=True, indexed=True) # ApartCIChangeset IDs # other properties I recently added a new type of query for these entities: keys = ApartCILabel.query(ApartCILabel.project_id == self.db_data.project_id, ApartCILabel.changeset_ids == self.key_id).fetch(keys_only=True) if keys: label = Label(db_key=keys[0], handler=self.handler) logging.debug('label found: %s' % label.name) else: logging.error('label for %s not found' % self.lid) I knew I needed a

db.ReferenceProperty() vs ndb.KeyProperty in App Engine

依然范特西╮ 提交于 2019-11-29 06:26:57
问题 ReferenceProperty was very helpful in handling references between two modules. Fox example: class UserProf(db.Model): name = db.StringProperty(required=True) class Team(db.Model): manager_name = db.ReferenceProperty(UserProf, collection_name='teams') name = db.StringProperty(required=True) To get 'manager_name' with team instance, we use team_ins.manager_name. To get 'teams' which are managed by particular user instance, we use user_instance.teams and iterate over. Doesn't it look easy and

Google App Engine: ImportError: No module named appengine.ext

走远了吗. 提交于 2019-11-29 05:08:49
I am trying to write a test for my GAE programme which uses the datastore. Following Google's Documentation , I see that I should be adding the path to my SDK into my PYTHONPATH. I did this using: import sys sys.path.remove('/usr/local/lib/python2.7/dist-packages') # Has a 'google' module, which I want to be sure isn't interfering. sys.path.insert(1,'/home/olly/google-cloud-sdk/platform/google_appengine') sys.path.insert(1, '/home/olly/google-cloud-sdk/platform/google_appengine/lib/yaml/lib') Then when the file is run: Traceback (most recent call last): File "myapp_tests.py", line 20, in

Google App Engine ndb equivalent for modelname_set (backreference property)

喜欢而已 提交于 2019-11-29 04:13:16
Is there an equivalent for modelname_set (a back-referenced property) in Google App Engine's NDB ? In the old DB a Model entity had described the back-reference property as : The name of the back-reference property defaults to modelname_set (with the name of the model class in lowercase letters, and "_set" added to the end), and can be adjusted using the collection_name argument to the ReferenceProperty constructor. I noticed this property does not seem to exist with NDB db.Model instances. Does NDB have an equivalent to the back-reference property? There is no direct back-reference properties

ndb query error with datetime field - Google App Engine

自古美人都是妖i 提交于 2019-11-29 03:57:21
I'm having a problem and I don't find any information about. I define a field in my model like this. class Dates(ndb.model): ... date = ndb.DateTimeProperty(required = True) # I want to store date and time ... Later I try a query (now I want all the dates for a day, I don'tn mind the time): kl = Dates.query(ndb.AND(Dates.date.year == year, Dates.date.month == month, Dates.date.day == day), ancestor = customer.key).fetch(keys_only = True) dates = ndb.get_multi(kl) But I get this error log: AttributeError: 'DateTimeProperty' object has no attribute 'year' I don't know why. I've tried Dates.date(

Maintain uniqueness of a property in the NDB database

China☆狼群 提交于 2019-11-29 01:45:35
An NDB model contains two properties: email and password . How to avoid adding to the database two records with the same email ? NDB doesn't have UNIQUE option for a property, like relational databases do. Checking that new email is not in the database before adding—won't satisfy me, because two parallel processes can both simultaneously do the checking and each add the same email . I'm not sure that transactions can help here, I am under this impression after reading some of the manuals. Maybe the synchronous transactions ? Does it mean one at a time? Create the key of the entity by email,

Google App Engine error: NeedIndexError: no matching index found

醉酒当歌 提交于 2019-11-29 01:23:52
I'm having trouble with Google's App engine indexes. When running my app via the GoogleAppEngineLauncher, the app is working fine. When deploying the app, I get the following error: NeedIndexError: no matching index found. The suggested index for this query is: - kind: Bar ancestor: yes properties: - name: rating direction: desc The error is generated after this line of code: bars = bar_query.fetch(10) Before the above line of code, it reads: bar_query = Bar.query(ancestor=guestbook_key(guestbook_name)).order(-Bar.rating) My index.yaml file contains the exact "suggested" index below #