开发者

App Engine Datastore join with filter on reference

开发者 https://www.devze.com 2023-03-19 19:40 出处:网络
I\'m very new to App Engine Datastore and I could not figure this out. I have these models: class SomeUser(User):

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

0

精彩评论

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

关注公众号