How to make a light query for a many to many relationship in Google Apps Engine?
How to make a light query for a many to many relationship?
Users has many Lists
the ListUser
i开发者_如何学JAVAs the model that links them
Currently I'm doing like this but there are a lot of get queries to get all this data.
lists = []
for list in user.lists:
lists.append(list.list)
Now I got this:
list_users = user.lists.fetch(1000)
# problem is here: "list.list" will retrive data from database, but I just want that list's key, how to do?
list_keys = [list.list for list in list_users]
lists = List.get_by_key_name(list_keys)
You should use get_value_for_datastore.
list_keys = [ListUser.list.get_value_for_datastore(list_user)
for list_user in list_users]
lists = db.get(list_keys)
If you have not already, you might want to take a look at some of the 'mastering the datastore' articles. Specifically the one on modeling relationships. Perhaps you can accomplish what you need using a list of List keys instead, maybe it will save you a query.
精彩评论