Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3285684
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:17:40+00:00 2026-05-17T20:17:40+00:00

Hi I am trying to build an application which has models resembling something like

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:17:41+00:00Added an answer on May 17, 2026 at 8:17 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build an Eclipse application that would work with a linux/motif
I'm trying to build an application in xcode, which -beside others- reads an rss
I'm currently trying to build an application that inherently needs good time synchronization across
Trying to build a GUI application in Java/Swing. I'm mainly used to painting GUIs
I'm trying to build a console application without using the CRT, or any other
I am trying to build an iPhone web application using ASP.NET. The page is
I've been trying to build a simple prototype application in Django, and am reaching
I'm trying to build a reusable $.ajax method for my current application but I'm
I'm trying to use NAnt/NAntContrib build script to build a web application on Windows
I am trying to build a simple drop down list for my MVC application.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.