App Engine Datastore join with filter on reference
I'm very new to App Engine Datastore and I could not figure this out.
I have these models:
class SomeUser(User):
name = db.StringProperty()
class Group(db.Model):
title = db.StringProperty()
date_started = db.DateTimeProperty(auto_now_add=True)
class GroupParticipant(db.Model):
group = db.ReferenceProperty(Group, collection_name = 开发者_C百科'participants')
participant = db.ReferenceProperty(SomeUser)
is_owner = db.BooleanProperty()
How to query the datastore to get a result like this:
Group.title, Group_owner, Number of participants/Group
group1, someuser1, 3
group2, someuser2, 4
There is no joins at all. You have to fetch all data manually. For example you can fetch all records from GroupParticipant
, and then fetch all Group
and SomeUser
in loop (note that fetching by Key
is very fast)
--
btw, probably it's better to use different data structures, like
class SomeUser(User):
name = db.StringProperty()
class Group(db.Model):
title = db.StringProperty()
date_started = db.DateTimeProperty(auto_now_add=True)
owner = db.ReferenceProperty(SomeUser)
participants = db.ListProperty(db.Key)
at this case you can fetch all required data by just one query
精彩评论