Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

modelling the google datastore/python

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!

like image 302
Rasmus Avatar asked Jul 23 '26 07:07

Rasmus


2 Answers

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.

like image 78
Robert Kluin Avatar answered Jul 25 '26 20:07

Robert Kluin


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).

like image 32
systempuntoout Avatar answered Jul 25 '26 22:07

systempuntoout



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!