Alexander Schwarzman
Alexander Schwarzman

Reputation: 1639

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.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.

Upvotes: 0

Views: 112

Answers (4)

Alexander Schwarzman
Alexander Schwarzman

Reputation: 1639

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.

Upvotes: 0

Will Curran
Will Curran

Reputation: 7110

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.

Upvotes: 0

Drew Sears
Drew Sears

Reputation: 12838

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)

Upvotes: 1

Abdul Kader
Abdul Kader

Reputation: 5842

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

Upvotes: 0

Related Questions