app-engine-ndb

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

社会主义新天地 提交于 2019-12-04 03:09:19
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? 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

One-To-Many Example in NDB

泪湿孤枕 提交于 2019-12-04 01:11:16
问题 I am trying to create ndb.Model class like Students and subjects class Subject(ndb.Model): name = ndb.StringProperty() class Student(ndb.Model): name = ndb.StringProperty() subject = ndb.KeyProperty(kind=Subject) One Student can have many Subjects. How to add and store these in this Model. I could not find any example of it. For String Property .. there is field property i.e. repeat=true How to achieve this and is there any working example on the web. Sorry if it is duplicate question but I

Loading datastore entities from Python project in Go leads to nested structs slices of slices error

丶灬走出姿态 提交于 2019-12-03 21:08:08
问题 I am writing a module in my Google AppEngine project in Go for performance reasons but need to be able to read from some of the entities I have in datastore. I wrote out the Go code to be able to read the entities I built out in Python but I am getting the following error: datastore: flattening nested structs leads to a slice of slices: field "Messages" Model Definitions in Python: class ModelB(ndb.Model): msg_id = ndb.StringProperty(indexed=False) cat_ids = ndb.StringProperty(repeated=True,

How to delete all entities for NDB Model in Google App Engine for python?

与世无争的帅哥 提交于 2019-12-03 15:35:49
问题 I have a ndb model class: class Game(ndb.Model): gameID = ndb.IntegerProperty() gameName = ndb.StringProperty() Is there any way to quickly just delete all entities thats stored in the database for this class? Something like Game.deletAll() 回答1: No, but you could easily do this with something like: from google.appengine.ext import ndb ndb.delete_multi( Game.query().fetch(keys_only=True) ) 来源: https://stackoverflow.com/questions/18945109/how-to-delete-all-entities-for-ndb-model-in-google-app

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

狂风中的少年 提交于 2019-12-03 15:28:23
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? 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 max is 5000 right now) you probably want to have the member model point to the events models (I am assuming

What is the correct way to get the previous page of results given an NDB cursor?

蹲街弑〆低调 提交于 2019-12-03 11:40:39
问题 I'm working on providing an API via GAE that will allow users to page forwards and backwards through a set of entities. I've reviewed the section about cursors on the NDB Queries documentation page, which includes some sample code that describes how to page backwards through query results, but it doesn't seem to be working as desired. I'm using GAE Development SDK 1.8.8. Here's a modified version of that example that creates 5 sample entities, gets and prints the first page, steps forward

What's the best way to specify a key_name for App Engine NDB Model?

懵懂的女人 提交于 2019-12-03 08:04:16
I'm trying to create an ndb model where each record has an unique field "name". I would like to define this field as the key_name field and use it to look up the records. Do I have to include a name field or can I somehow set the key_name field to an arbitrary string that the user can specify as long as it's unique? I'm thinking of using Model.get_or_insert to make sure that old records don't get overwritten, but is there a way to tell if the return value is newly created or pre-existing? I want to be able to display an error message if the user entered a duplicate name. Lastly, I tried to

Following backreferences of unknown kinds in NDB

…衆ロ難τιáo~ 提交于 2019-12-03 07:18:22
I'm in the process of writing my first RESTful web service atop GAE and the Python 2.7 runtime; I've started out using Guido's shiny new ndb API. However, I'm unsure how to solve a particular case without the implicit back-reference feature of the original db API. If the user-agent requests a particular resource and those resources 1 degree removed: host/api/kind/id?depth=2 What's the best way to discover a related collection of entities from the "one" in a one-to-many relationship, given that the kind of the related entity is unknown at development time? I'm unable to use a replacement query

How do I know if ndb.Model.get_or_insert created a new entity or got an existing one?

↘锁芯ラ 提交于 2019-12-03 06:11:08
For the following (broken) function, I want to return True if the entity was created or updated, and False otherwise. The problem is that I do not know whether get_or_insert() got an existing entity, or inserted one. Is there an easy way to determine this? class MyModel(ndb.Model): def create_or_update(key, data): """Returns True if entity was created or updated, False otherwise.""" current = MyModel.get_or_insert(key, data=data) if(current.data != data) current.data = data return True return False get_or_insert() is a trivial function (although its implementation looks complex, because it

ndb to_dict method does not include object's key

断了今生、忘了曾经 提交于 2019-12-03 04:27:15
问题 I am leveraging ndb's to_dict method to convert an object's properties into a python dict. From everything I can tell, this method does not include the object's key or parent within the dict as per the documentation: https://developers.google.com/appengine/docs/python/ndb/modelclass#Model_to_dict However for my situation I need the key to be in the dict. My preference would be to leverage the builtin method and subclass it or something similar rather than create my own to_dict method. What is