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 4341184
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:27:02+00:00 2026-05-21T11:27:02+00:00

A] Problem Summary: I have one to many data models in my project. I

  • 0

A] Problem Summary:

I have one to many data models in my project. I need help constructing a query to get the “many” side data object based on the key of the “one” side data object.

Please refer to “EDIT#1” for the code that has worked, but still inefficient.

B] Problem details:

1] I have “UserReportedCountry” (one) to “UserReportedCity” (many) , “UserReportedCity” (one) to “UserReportedStatus” (many) data model relationships.

2] The key for UserReportedCountry is a country_name , example — “unitedstates”.
The key for UserReportedCity is country_name:city_name, example “unitedstates:boston.
UserReportedStatus has no special key name.

3] I have the users country and city name in my python code and I want to retrieve all “UserReportedStatus” objects based on the city key name which is “county_name:city_name”

C] Code excerpts:

1] database models:

class UserReportedCountry(db.Model):
  country_name = db.StringProperty( required=True,
                          choices=['Afghanistan','Aring land Islands']
                         )

class UserReportedCity(db.Model):
  country = db.ReferenceProperty(UserReportedCountry, collection_name='cities')
  city_name = db.StringProperty(required=True)   

class UserReportedStatus(db.Model):
  city = db.ReferenceProperty(UserReportedCity, collection_name='statuses')
  status = db.BooleanProperty(required=True)
  date_time = db.DateTimeProperty(auto_now_add=True)

2] query I have tried so far:

def get_data_for_users_country_and_city(self,users_country,users_city):
    key_name_for_user_reported_city = users_country + ":" + users_city
    return UserReportedStatus.all().filter('city.__key__=', key_name_for_user_reported_city ).fetch(limit=10)

D] Technologies being used

1] Python

2] Google App engine

3] Django

4] Django models.

[EDIT#1]

I have tried the following mechanism to query the status objects based on the given city and country. This has worked, but i believe this is an inefficient mechanism to perform the task.

def get_data_for_users_country_and_city(self,users_country,users_city):
    key_name_for_user_reported_city = users_country + ":" + users_city
    city_object = UserReportedCity.get_by_key_name( key_name_for_user_reported_city )
    return UserReportedStatus.gql('WHERE city=:1', city_object)
  • 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-21T11:27:02+00:00Added an answer on May 21, 2026 at 11:27 am

    Your last query looks correct and I don’t see any problem of inefficiency on it; in fact querying by_key_name is pretty fast and efficient.

    I think you have room of improvement on your normalized RDMBS oriented Models design instead; since GAE does not support JOINS, after the get_data_for_users_country_and_city call, you will end in hitting the datastore too much to fetch the city_name and country_name properties when dereferenced.

    What could you do? Denormalization and prefetching.

    1. Add a country_name property in the UserReportedCity model definition
    2. Prefetch ReferenceProperty to retrieve the UserReportedCity object for each UserReportedStatus object
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem I have timestamped data, which I need to search based on the timestamp
I have got a problem on casting an object to one of it's base
Problem: I have two spreadsheets that each serve different purposes but contain one particular
I have been looking at many ASP.Net MVC client side validation ideas including xVal.
Summary A parent can have many children. How do you write a service such
I have a problem with nhibernate when I try to insert an object with
Summary We have an ASP.NET application that allows users to query a SQL Server
Problem: I have an address field from an Access database which has been converted
Problem (simplified to make things clearer): 1. there is one statically-linked static.lib that has
Problem solved: Thanks guys, see my answer below. I have a website running in

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.