app-engine-ndb

How to set an ndb keyProperty

◇◆丶佛笑我妖孽 提交于 2019-12-05 19:22:42
I'm having some trouble understanding how entities and keys work in Google App Engine NDB. I have a post entity and a user entity. How do I set the user_key on post to user ? In the interactive console, I have this so far: from google.appengine.ext import ndb from app.lib.posts import Post from app.lib.users import User from random import shuffle users = User.query() posts = Post.query().fetch() for post in posts: post.user_key = shuffle(users)[0] post.put() I'm just trying to set up some seed data for development. I know this probably isn't the ideal way to set things, but my first question

App Engine return JSON from JsonProperty

烂漫一生 提交于 2019-12-05 11:39:55
I like how the JsonProperty automatically encodes a Python structure into JSON when the property is put into the data store, and automatically decodes it when retrieved. However, it would be nice to send that JSON data to a web browser without having to encode it again. Is there a way to get the raw JSON data (that is, prevent the decoding)? class DataForBrowser(ndb.Models) json = ndb.JsonProperty() def get_json(self): return ??? So what you want is to have a dict that gets encoded when saved to the datastore but not decoded upon retrieving it... What happens "under the hood" is that a

Does the NDB membership query (“IN” operation) performance degrade with lots of possible values?

泄露秘密 提交于 2019-12-05 10:35:11
The documentation for the IN query operation states that those queries are implemented as a big OR'ed equality query: qry = Article.query(Article.tags.IN(['python', 'ruby', 'php'])) is equivalent to: qry = Article.query(ndb.OR(Article.tags == 'python', Article.tags == 'ruby', Article.tags == 'php')) I am currently modelling some entities for a GAE project and plan on using these membership queries with a lot of possible values: qry = Player.query(Player.facebook_id.IN(list_of_facebook_ids)) where list_of_facebook_ids could have thousands of items. Will this type of query perform well with

Get a random entity in Google App Engine datastore which does not belong to a list

大兔子大兔子 提交于 2019-12-05 06:20:59
问题 I am using Google App Engine to build a web game. The game has a list of stored user-created levels (which may be a lot, and dynamically increasing as well), and each user has a list of levels which he has played already. I need to randomly pick up a level for a user which he has not played before. The Entities are modeled like this: class User(ndb.Model): uid = ndb.StringProperty() levels_played = ndb.KeyProperty(kind='Level', repeated=True) class Level(ndb.Model): #some stuff here So

How to read old property values in a _pre_put_hook

送分小仙女□ 提交于 2019-12-05 03:04:39
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('UPDATE: {0}'.format(attr)) logging.debug('OLD: {0} NEW: {1}'.format(getattr(old_object, attr), getattr

Is it best to query by keys_only=True then get_multi or just full query?

荒凉一梦 提交于 2019-12-05 01:31:22
I am using NDB with python 2.7 with threadsafe mode turned on. I understand that querying for entities with NDB does not use local cache or memcache but goes straight to the datastore unlike getting by key name. (The rest of the question might be redundant if this premise is not correct.) Therefore would a good paradigm be to only query with keys_only=True and then do a get_multi to obtain the full entities? The benefits would be that keys_only=True queries are much faster than keys_only=False, get_multi could potentially just hit memcache & by calling get_multi your entities are now saved in

dowload app engine ndb entities via bulk exporter / bulk uploader

不问归期 提交于 2019-12-04 22:48:54
Context: My model classes inherit from a base class: class BaseModel(ndb.model): # commom fields and methods class SpecificModel(BaseModel): # specific fields and methods Problem: I want to export the SpecificModel entities using the appengine bulkuploader service . I have the defined the config file (data_loader.py): import sys sys.path.append('.') ## this is to ensure that it finds the file 'models.py' from google.appengine.ext import ndb from google.appengine.tools import bulkloader from models import * class SpecificModelExporter(bulkloader.Exporter): def __init__(self): bulkloader

Custom properties not saved correctly for Expando models in repeated StructuredProperty

丶灬走出姿态 提交于 2019-12-04 19:00:46
I am trying to use an Expando model as a repeated StructuredProperty in another model. Namely, I would like to add an indefinite number of Accounts to my User model. As Accounts can have different properties depending on their types ( Accounts are references to social network accounts, and for example Twitter requires more information than Facebook for its OAuth process), I have designed my Account model as an Expando . I've added all basic information in the model definition, but I plan to add custom properties for specific social networks (e.g., a specific access_token_secret property for

App Engine instance memory constantly increasing

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-04 18:09:29
I'd expect the memory usage of my app engine instances (Python) to be relatively flat after an initial startup period. Each request to my app is short lived, and it seems all memory usage of single request should be released shortly afterwards. This is not the case in practice however. Below is a snapshot of instance memory usage provided by the console. My app has relatively low traffic so I generally have only one instance running. Over the two-day period in the graph, the memory usage trend is constantly increasing. (The two blips are where two instances were briefly running.) I regularly

How to fetch the latest data in GAE Python NDB

半世苍凉 提交于 2019-12-04 17:25:58
I am using GAE Python. I have two root entities: class X(ndb.Model): subject = ndb.StringProperty() grade = ndb.StringProperty() class Y(ndb.Model): identifier = ndb.StringProperty() name = ndb.StringProperty() school = ndb.StringProperty() year = ndb.StringProperty() result = ndb.StructuredProperty(X, repeated=True) Since google stores our data across several data centers, we might not get the most recent data when we do a query as shown below(in case some changes have been "put"): def post(self): identifier = self.request.get('identifier') name = self.request.get('name') school = self