NDB using Users API to form an entity group

你离开我真会死。 提交于 2019-12-12 22:08:40

问题


I'm trying to wrap my head around what seems to be a very simple use case, but I seem to be failing miserably. The goal of the exercise is to look up a set of records for the user that logs in using the Google Accounts username within the high replication datastore and be extremely consistent.

My data looks like this:

class Account(ndb.Model):
    owner = ndb.UserProperty()
    name = ndb.StringProperty()

class Content(ndb.Model):
    content = ndb.StringProperty()

When I first create the account, I just do the following:

current_user = users.get_current_user()
new_account = Account(owner=current_user, name='Some Name')
new_account.put()

Now create content:

new_content = Content(parent=new_account.key, content='Some Content')
new_content.put()

When the user logs in, I can only query by UserProperty, but I seem to be unable to set user as the parent for an entity group, so how do I make sure that I always can look-up the Account by the logged in user and be extremely consistent?

Once I have the account, the ancestor query process will ensure consistency for content retrieval, but I'm stuck on figuring out ancestry based on User.


回答1:


The easiest and best way to do this is using key names. In this case, the key name for your Account entity should be the ID of the user. You can create accounts like this:

new_account = Account(owner=current_user, id=current_user.user_id(), name='Some Name')

And you can look them up like this:

existing_account = Account.get_by_id(current_user.user_id())



回答2:


I'm not sure why exactly you want to do that, but you have to create a parent entity for Account as well, so that you can perform queries on it with an ancestor.



来源:https://stackoverflow.com/questions/10271103/ndb-using-users-api-to-form-an-entity-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!