如何按 ReferenceProperty 模型的名称进行过滤?

发布于 2024-11-06 03:35:15 字数 451 浏览 1 评论 0原文

我有以下 3 个类:

class Locality(db.Model):
  location = db.ReferenceProperty()

class Organisation(db.Model):
  locality = db.ReferenceProperty(Locality)

class Position(db.Model):
  locality = db.ReferenceProperty(Locality)

Locality.location 属性是对 Organization 或 Position 对象的引用。 我需要按地点过滤并获取参考组织的所有条目。

我已经尝试过,但行不通:

Locality.all().filter("location =",Organisation)

任何建议将不胜感激。

I have the following 3 classes:

class Locality(db.Model):
  location = db.ReferenceProperty()

class Organisation(db.Model):
  locality = db.ReferenceProperty(Locality)

class Position(db.Model):
  locality = db.ReferenceProperty(Locality)

Locality.location property is reference to Organisation or Position object.
I need to filter by Locality and get all entries with reference to Organisation.

I already tried, but it won't work:

Locality.all().filter("location =",Organisation)

Any advices will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怂人 2024-11-13 03:35:15

在这一行中:

Locality.all().filter("location =", Organisation)

您应该传递 Organization 的实例而不是类本身,例如:

org = Organisation.get(some_org_key)
Locality.all().filter("location =", org)

In this line:

Locality.all().filter("location =", Organisation)

You should be passing in an instance of Organisation rather then the class itself, e.g.:

org = Organisation.get(some_org_key)
Locality.all().filter("location =", org)
天荒地未老 2024-11-13 03:35:15
class Locality(db.Model):
  location = db.ReferenceProperty()

class Organisation(db.Model):
  locality = db.ReferenceProperty(Locality,collection_name='org')

class Position(db.Model):
  locality = db.ReferenceProperty(Locality,collection_name='pos')

现在每个 locality 对象都会有一个 loc 属性,它只不过是 Organization 的集合。

要了解有关建模的更多信息,请浏览此博客< /a>

class Locality(db.Model):
  location = db.ReferenceProperty()

class Organisation(db.Model):
  locality = db.ReferenceProperty(Locality,collection_name='org')

class Position(db.Model):
  locality = db.ReferenceProperty(Locality,collection_name='pos')

now each and every locality object will have an loc attribute, which is nothing but an collection of Organisation.

To know more about modeling , go through this blog

不爱素颜 2024-11-13 03:35:15

与德鲁的回答相比,我更喜欢阿卜杜勒的方法(尽管他正确地回答了您的具体问题)。我不知道这里的所有移动部件,但我怀疑您想要以不同的方式对其进行建模,也许使用 PolyModel 和一些反规范化。我的意思是,只要看看这四个类的关系,我就会发现很多简单的查询都在进行去引用。

I like Abdul's approach better than Drew's answer (although he answers your specific question correctly). I don't know all the moving parts here but I suspect you want to model this a bit differently, perhaps using PolyModel, and some de-normalization. I mean just looking at the relationships of these four classes, I see a lot of de-refrencing going on just for simple queries.

誰ツ都不明白 2024-11-13 03:35:15

最后,我找到了基于PolyModels的解决方案:

class Locality(polymodel.PolyModel):
  { geo properties here }

class Organisation(Locality):
  title = db.StringProperty()

class Position(Locality):
  title = db.StringProperty()

要按地理属性过滤所有组织:

Organisation.all().filter({by geo properties of Locality model})

要按地理属性过滤所有地点(组织+职位):

Locality.all().filter({by geo properties of Locality model})

我想,我最初的解释非常不清楚。对此感到抱歉。谢谢大家的建议。它们非常有用。

Finally, I found the solution based on PolyModels:

class Locality(polymodel.PolyModel):
  { geo properties here }

class Organisation(Locality):
  title = db.StringProperty()

class Position(Locality):
  title = db.StringProperty()

To get all organisations filtered by geo properties:

Organisation.all().filter({by geo properties of Locality model})

To get all localities (organisations + positions) filtered by geo properties:

Locality.all().filter({by geo properties of Locality model})

I guess, my initial explanation was very unclear. Sorry for that. And thank you all, for your advices. They were very useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文