如何根据从令牌中获得的 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 键的键创建一个查询祖先。我不想使用该解决方案在我的模型中添加字段所有者,因为查询祖先比过滤器更快,它确实帮助我组织我的数据库。
谢谢