views:

85

answers:

2

Hi I am trying to build an application which has models resembling something like the below ones:-(While it would be easy to merge the two models into one and use them , but that is not feasible in the actual app)

class User(db.Model):
     username=db.StringProperty()
     email=db.StringProperty()

class UserLikes(db.Model):
      username=db.StringProperty()
      food=db.StringProperty()

The objective- The user after logging in enters the food that he likes and the app in turn returns all the other users who like that food. Now suppose a user Alice enters that she likes "Pizzas" , it gets stored in the datastore. She logs out and logs in again.At this point we query the datastore for the food that she likes and then query again for all users who like that food. This as you see are two datastore queries which is not the best way. I am sure there would definitely be a better way to do this. Can someone please help.

[Update:-Or can something like this be done that I change the second model such that usernames become a multivalued property in which all the users that like that food can be stored.. however I am a little unclear here]

[Edit:-Hi Thanks for replying but I found both the solutions below a bit of a overkill here. I tried doing it like below.Request you to have a look at this and kindly advice. I maintained the same two tables,however changed them like below:-

class User(db.Model):
    username=db.StringProperty()
    email=db.StringProperty()

class UserLikes(db.Model):
     username=db.ListProperty(basestring)
     food=db.StringProperty()

Now when 2 users update same food they like, it gets stored like

'pizza' ----> 'Alice','Bob'

And my db query to retrieve data becomes quite easy here

query=db.Query(UserLikes).filter('username =','Alice').get()

which I can then iterate over as something like

    for elem in query.username:
          print elem

Now if there are two foods like below:-

'pizza' ----> 'Alice','Bob'
'bacon'----->'Alice','Fred'

I use the same query as above , and iterate over the queries and then the usernames.

I am quite new to this , to realize that this just might be wrong. Please Suggest!

+1  A: 

Food and User relation is a so called Many-to-Many relationship tipically handled with a Join table; in this case a db.Model that links User and Food. Something like this:

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

  def get_food_I_like(self):
     return (entity.name for entity in self.foods)

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

  def get_users_who_like_me(self):
    return (entity.name for entity in self.users)

class UserFood(db.Model):
  user= db.ReferenceProperty(User, collection_name='foods')
  food = db.ReferenceProperty(Food, collection_name='users')

For a given User's entity you could retrieve preferred food with:

userXXX.get_food_I_like()

For a given Food's entity, you could retrieve users that like that food with:

foodYYY.get_users_who_like_me()

There's also another approach to handle many to many relationship storing a list of keys inside a db.ListProperty().

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

class User(db.Model):
  name = db.StringProperty()
  food = db.ListProperty(db.Key)

Remember that ListProperty is limited to 5.000 keys or again, you can't add useful properties that would fit perfectly in the join table (ex: a number of stars representing how much a User likes a Food).

systempuntoout
Be very careful using that `get_food_I_like` function. It will do an additional datastore call for *every* user who likes the food.
Robert Kluin
Thanks systempuntoout.. but this again uses one more datastore call. For eg:-after the user logs in, I will have to do something like food=userxx.get_food_i_like() , and then again for finding all the users who like the food, users=foodYYY.get_users_who_like_me().The objective is to display all the users in a small display when the user logs in. I tried doing something which works , but then I might be quite wrong too, would be good if someone could have a look at it
Alice
Request you to have a look at a solution I worked out in the original post.Please Advice!
Alice
+2  A: 

Beside the relation model you have, you could handle this in two other ways depending on your exact use case. You have a good idea in your update, use a ListProperty. Check out Brett Slatkin's taslk on Relation Indexes for some background.

You could use a child entity (Relation Index) on user that contains a list of foods:

class UserLikes(db.Model):
    food = db.StringListProperty()

Then when you are creating a UserLikes instance, you will define the user it relates to as the parent:

likes = UserLikes(parent=user)

That lets you query for other users who like a particular food nicely:

like_apples_keys = UserLikes.all(keys_only=True).filter(food='apples')
user_keys = [key.parent() for key in like_apples_keys]
users_who_like_apples = db.get(user_keys)

However, what may suit your application better, would be to make the Relation a child of a food:

class WhoLikes(db.Model):
    users = db.StringListProperty()

Set the key_name to the name of the food when creating the like:

food_i_like = WhoLikes(key_name='apples')

Now, to get all users who like apples:

apple_lover_key_names = WhoLikes.get_by_key_name('apples')
apple_lovers = UserModel.get_by_key_names(apple_lover_key_names.users)

To get all users who like the same stuff as a user:

same_likes = WhoLikes.all().filter('users', current_user_key_name)
like_the_same_keys = set()
for keys in same_likes:
   like_the_same_keys.union(keys.users)
same_like_users = UserModel.get_by_key_names(like_the_same_keys)

If you will have lots of likes, or lots users with the same likes, you will need to make some adjustments to the process. You won't be able to fetch 1,000s of users.

Robert Kluin
Wonderful tutorial at that link.. thanks.. please have a look at my edited solution in the post above. I am sure I should have done some goof up!
Alice
You are effectively doing what I suggested. One of the biggest differences is that you are using a property to store the value for `food` instead of using the key_name. Using the key_name will be more efficient when your trying to find all users who like food `x`. However, if your lists will contain over a few thousand users it will probably be easiest to include a property. Make sure you're using Appstats (http://code.google.com/appengine/docs/python/tools/appstats.html) so you can see exactly what is happening behind the scenes.
Robert Kluin
Thanks!Robert ..
Alice
@Robert what if a food is liked by more than 5000 users?
systempuntoout
One common way to handle the over 5000 items case is adding a 'shard.' Basically, add another RelationIndex entity who's key_name is something like 'apples;1'. You'll just need to add a bit more logic to handle those cases. It might look something like: if there are 5,000 users in a list, add the next shard (build the known key_name) to a fetch list, then repeat. You could also add a shard count so you could easily build all of the key_names up front. But are you really going to fetch over 5,000 users at once?
Robert Kluin