Julius F
Julius F

Reputation: 3444

AppEngine Python getting all references

My problem is simple. I have Model_A and Model_B:

class Lobby(db.Model):
    name = db.StringProperty()  

class UserModel(db.Model):
    name = db.StringProperty()
    lobby = db.ReferenceProperty(Lobby, collection_name="lobby")

Now I fetch a/all Lobby entities from the Storage, and I want to access the related users for each Lobby. I can not do that by calling lobby_entity.aUsers.

How is this achieved?

Upvotes: 0

Views: 112

Answers (1)

Guido van Rossum
Guido van Rossum

Reputation: 16890

Using GQL:

lobbies = Lobby.all().fetch(10)
keys = [lobby.key() for lobby in lobbies]
q = aUser.gql('WHERE lobby IN :1', keys)
users = q.fetch(1000)

Note that this doesn't work well if you have a lot of lobbies; the IN query only supports up to 30 values. (https://developers.google.com/appengine/docs/python/datastore/gqlreference)

PS. Please don't start model class names with a lowercase letter. Check out PEP 8.

Upvotes: 1

Related Questions