如何根据我从令牌获取的user_id进行祖先的查询?
这是我的代码:
class MyUserClass(EndPointModel):
user_object = ndb.UserProperty(required=True, indexed=False)
class MyModel(EndPointModel):
...
@MyModel.method(user_required=True, name="model.add", path="model")
def add_mymodel(self, my_model):
gplus_user_id = auth_util.get_google_plus_user_id()
if gplus_user_id is None:
raise endpoints.ForbiddenException(NidAppUser.NO_GPLUS_ID)
user = MyUserClass.get_or_insert(gplus_user_id, user_object=endpoints.get_current_user())
my_model.parent = user.key.string_id()
my_model.put()
return my_model
@MyModel.query_method(user_required=True, name="list.mymodel", path="models",
query_fields=('order','attr1',))
def list_models(self, query):
gplus_user_id = auth_util.get_google_plus_user_id()
if gplus_user_id is None:
raise endpoints.ForbiddenException(NidAppUser.NO_GPLUS_ID)
## Here or in thsi decorator function I want to do something like
## query.ancestor.(ndb.Key(MyClassUser, gplus_user_id)) (such as the DB Datastore)
## to return only the query of MyModels that belong to the current user
return query
我需要通过MyUserClass键的键创建一个查询祖先。我不想使用该解决方案在我的模型中添加字段所有者,因为查询祖先比过滤器更快,它真的帮助我组织我的数据库。
谢谢
答案 0 :(得分:0)
您可以构建基于祖先的查询,如下所示:
myancestorkey = ndb.Key(MyUserClass, gplus_user_id)
myquery = MyModel.query(ancestor=myancestorkey)
答案 1 :(得分:0)
使用endpoints_proto_datastore,您可以执行以下操作:
@MyModel.query_method(user_required=True, name="list.mymodel",
path="models", query_fields=('order','attr1',))
def list_models(self, query):
# Returns only MyModels that belong to the current g+ user
return query.filter(MyModel.parent == auth_util.
get_google_plus_user_id())