简体   繁体   English

参考属性过滤器

[英]ReferenceProperty filter

I have a List kind, and an User kind.我有一个列表类型和一个用户类型。 I'm currently using IntegerProperty to associate User IDs with Lists, but I want to switch to ReferenceProperty.我目前正在使用 IntegerProperty 将用户 ID 与列表相关联,但我想切换到 ReferenceProperty。 Currently, I'm using this code (with IntegerProperty):目前,我正在使用此代码(使用 IntegerProperty):

db.GqlQuery("SELECT * FROM List WHERE UserID = :1", userid)

How should the code with ReferenceProperty look like? ReferenceProperty 的代码应该是什么样子的? The script has the numeric ID of User (userid).该脚本具有用户的数字 ID (userid)。

First, you need to construct a key from your ID.首先,您需要根据您的 ID 构造一个密钥。 You can do that like this (presuming your User entity has no parent):您可以这样做(假设您的 User 实体没有父级):

user_key = db.Key.from_path('UserInfo', user_id)

Now you can use it in a query just as you would anything else:现在您可以在查询中使用它,就像使用其他任何东西一样:

db.GqlQuery("SELECT * FROM List WHERE user_key = :1", user_key)

Or equivalently with a Query instead of GQL:或者等效地使用查询而不是 GQL:

List.all().filter("user_key =", user_key)

With a ReferenceProperty you'll need an actual entity for it to reference.使用 ReferenceProperty,您需要一个实际的实体来引用它。 So the code would essentially be the same, except userid would not be an int, it would be an entity (or I think a key to an entity would work in this case).所以代码基本上是相同的,除了 userid 不是一个 int,它是一个实体(或者我认为一个实体的键在这种情况下可以工作)。 If you're trying to switch to ReferenceProperty I'm assuming you want to start using a User entity in your List instead of user id, so it would look something like...如果您尝试切换到 ReferenceProperty,我假设您想开始在列表中使用用户实体而不是用户 ID,所以它看起来像......

user = User.all()[0] # or some other thing to get your user
db.GqlQuery("SELECT * FROM List WHERE user = :1", user)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM