开发者

How to filter by name of ReferenceProperty's model?

I have the following 3 classes:

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

class Organisation(db.M开发者_Go百科odel):
  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.


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)


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜