如何按 ReferenceProperty 模型的名称进行过滤?
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在这一行中:
您应该传递 Organization 的实例而不是类本身,例如:
In this line:
You should be passing in an instance of Organisation rather then the class itself, e.g.:
现在每个 locality 对象都会有一个 loc 属性,它只不过是 Organization 的集合。
要了解有关建模的更多信息,请浏览此博客< /a>
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
与德鲁的回答相比,我更喜欢阿卜杜勒的方法(尽管他正确地回答了您的具体问题)。我不知道这里的所有移动部件,但我怀疑您想要以不同的方式对其进行建模,也许使用 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.
最后,我找到了基于PolyModels的解决方案:
要按地理属性过滤所有组织:
要按地理属性过滤所有地点(组织+职位):
我想,我最初的解释非常不清楚。对此感到抱歉。谢谢大家的建议。它们非常有用。
Finally, I found the solution based on PolyModels:
To get all organisations filtered by geo properties:
To get all localities (organisations + positions) filtered by geo properties:
I guess, my initial explanation was very unclear. Sorry for that. And thank you all, for your advices. They were very useful.