app-engine-ndb

Google Datastore queries and eventual consistency

强颜欢笑 提交于 2019-11-30 09:59:04
I would like to confirm my understanding of eventual consistency in the Google datastore. Suppose that I have an entity defined as follows (using ndb): class Record(ndb.Model): name = ndb.StringProperty() content = ndb.BlobProperty() I think I understand Scenarios 1, but I have doubts about Scenarios 2 and 3, so some advice would be highly appreciated. Scenario 1: I insert a new Record with name "Luca" and a given content. Then, I query the datastore: qry = Record.query(name=="Luca") for r in qry.iter(): logger.info("I got this content: %r" % r.content) I understand that, due to eventual

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

▼魔方 西西 提交于 2019-11-30 09:47:37
问题 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)

Does ndb.toplevel break transactions?

风流意气都作罢 提交于 2019-11-30 09:36:46
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? I discovered that the problems is that both create new contexts. transactional creates a context and ensures that all writes that happen inside of it are non

How to query parent entity from child entity in Google App Engine (Python) NDB/Datastore?

倖福魔咒の 提交于 2019-11-30 09:01:57
My question is very fundamental, I want to know straight forward and right way to access attribute values of parent entity from a child in App Engine Python. For example I have following model schema. I am using Python 2.7 and NDB. class Gallery(ndb.Model): category = ndb.StringProperty() title = ndb.StringProperty() subtitle = ndb.StringProperty() class Image(ndb.Model): blob_key = ndb.BlobKeyProperty() title = ndb.StringProperty() gallery = ndb.StringProperty() is_slider = ndb.StringProperty() Here "Gallery" is parent of "Image". They form an entity group Exhibition=>Gallery=>Image. I want

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

五迷三道 提交于 2019-11-30 06:54:11
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 understandable? In doing same thing using NDB, we have to modify db.ReferenceProperty(UserProf,

Why required and default are mutally exclusive in ndb?

旧城冷巷雨未停 提交于 2019-11-30 03:03:01
问题 In old google appengine datastore API "required" and "default" could be used together for property definitions. Using ndb I get a ValueError: repeated, required and default are mutally exclusive. Sample code: from google.appengine.ext import ndb from google.appengine.ext import db class NdbCounter(ndb.Model): # raises ValueError count = ndb.IntegerProperty(required=True, default=1) class DbCounter(db.Model): # Doesn't raise ValueError count = db.IntegerProperty(required=True, default=1) I

Google App Engine (Python) - Uploading a file (image)

安稳与你 提交于 2019-11-30 02:29:39
I want the user to be able to upload images to Google App Engine. I have the following (Python): class ImageData(ndb.Model): name = ndb.StringProperty(indexed=False) image = ndb.BlobProperty() Information is submitted by the user using a form (HTML): <form name = "input" action = "/register" method = "post"> name: <input type = "text" name = "name"> image: <input type = "file" name = "image"> </form> Which is then processed by: class AddProduct(webapp2.RequestHandler): def post(self): imagedata = ImageData(parent=image_key(image_name)) imagedata.name = self.request.get('name') imagedata.image

Google App Engine NDB custom key id

…衆ロ難τιáo~ 提交于 2019-11-30 01:08:41
问题 When I create an object with ndb's method put it creates the key automatically of the type Key(kind, id) where id is a number. All over the documentation it shows that you can use a string for the key's id but I couldn't find out how to do this automatically when an object is created. I have a User model and I was thinking to use the user's username (since its unique) as the key's id for faster retrieval. Is that even a good idea? Would I have any problems with the username since it's user

How to use AJAX with Google App Engine (Python)

谁说胖子不能爱 提交于 2019-11-29 23:19:12
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> {{story.vote_count}} | <a href="#">Vote Up Story</a> </div> How does AJAX fit inside here? Ok Sir here we go

what's the difference between google.appengine.ext.ndb and gcloud.datastore?

感情迁移 提交于 2019-11-29 17:44:27
问题 ndb: (from google.appengine.ext import ndb) datastore: (from gcloud import datastore) What's the difference? I've seen both of them used, and hints they both save data to google datastore. Why are there two different implementations? 回答1: The Python NDB Client is specific to Python Applications running on Google App Engine. The datastore client removes that restriction and you can run your Python application anywhere, including Google App Engine, Google Compute Engine or anywhere else.