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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:16:15+00:00 2026-05-21T02:16:15+00:00

A] Summary of the problem: I Have 1 to many hierarchical relationships between models

  • 0

A] Summary of the problem:

I Have 1 to many hierarchical relationships between models

Country (1) –> City (Many)
City (1) –> Status (Many)

So, there can only be one unique country, a country can only have one unique city, and a city could have many statuses

I am planning to use “get_or_insert” method to make sure that i maintain unique records in the database.

B] Code Excerpts:

1] Model structure —

class UserReportedCountry(db.Model):
  name = db.StringProperty(required=True)

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

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

2] Code that is used for storing the data retrieved from the HTML form:

def store_user_data(self): 
  country_name = self.request.get('selCountry')
  user_reported_country = UserReportedCountry.get_or_insert(name=country_name)

  user_reported_city =  UserReportedCity.get_or_insert( name = self.request.get('city'), country = user_reported_country )

  user_reported_status = UserReportedStatus( status = self.request.get('status'), city = user_reported_city)
    user_reported_status.put()      

Questions:

1] From the google search, it appears “get_or_insert” requires a key, In my case in the “UserReportedCountry” model, i want the name of the country to be the primary key
and in the “UserReportedCity” model, i want the combination of country name + city name to be the key. How do i go about doing this ?

2] Is there a way to use “get_or_insert” without specifying a key, I came accross the following posting on stackoverflow (http://stackoverflow.com/questions/4308002/google-app-engine-datastore-get-or-insert-key-name-confusion), and tried the idea but it didnt work.

Thank you for reading,

[EDIT#1]

Summary of the changes based on the response given by @Josh Smeaton

1] Now the code checks if the user reported country is present in the db or not. If the user reported country is not present, then the code creates a UserReportedCountry, UserReportedCity and attaches a new status to it

2] If the country is present, then the code checks whether the user reported city is present for the given country.

If the city is not found, then create a city record and associated it with the found country and attach a status record.

If the city is found, then attach the status record to it.

Request:

I will highly appreciate, If someone can please do a code review and let me know if i am making any mistakes.

Thanks,

Code excerpts:

#this method will be used to parse the data the user provided in the html form and store it in the database models
#while maintaing the relationship between UserReportedCountry, UserReportedCity and UserReportedStatus
#BUG, there needs to be error checking to make sure the country , city and status data is invalid or not
#if the data is invalid, then error message needs to be reported and then redirection back to the main page
def store_user_data(self):
    #method call to find out the completly filled out UserReportedCity model
    user_reported_city = self.find_or_create_user_reported_country_and_city(
                                self.request.get('selCountry'), self.request.get('city'))

    #status is always unique for a user entry, so create a brand new UserReportedStatus everytime.
    user_reported_status = UserReportedStatus(status = self.get_user_reported_status(), city = user_reported_city)
    user_reported_status.put()            

#Here the code needs to find out if there is an existing country/city for the user selection
#1] If the user reported country doesnt exist, create a new country record, create a new city record and return the city record
#2] If the user reported country exists, check if the user reported city is associated with the country. 
#if the city exists, then return it. If the city doesnt exists, then create a new city and return it  
#example: if the user chooses USA, there needs to be a check if USA is already present or not, 
#so that we dont create an additonal USA record
def find_or_create_user_reported_country_and_city(self, country_name, city_name):
    country_query_result = db.GqlQuery("SELECT * FROM UserReportedCountry WHERE name = :country_name_value" 
                                       ,country_name_value = country_name).get()

    if (country_query_result == None):
        #since the country doesnt exists, create and save the country
        user_reported_country = self.create_and_save_user_country_record(country_name)

        #Since the country doesnt exist, there cannot be a city record for the given country, so blindly create the record
        return self.create_and_save_user_city_record(city_name, user_reported_country)
    else:
        #Since we found a country, now we need to find whether the user selected city exists for the given country
        return self.find_or_create_city_for_country(country_query_result, city_name)

#Check wheter the user selectred city exists in the country
#1] if the city exists return the record back 
#2] if the city doesnt exist creaty the city record and return it   
def find_or_create_city_for_country(self, country_record, city_name):
    city_query_result = db.GqlQuery("SELECT * FROM UserReportedCity WHERE name = :city_name_value AND country =:country_value"
                                     ,city_name_value = city_name, country_value = country_record ).get()

    if (city_query_result == None):
        #Since the city doesnt exist for the given country, 
        #create the city record, associated it with the country and return the record back
        return self.create_and_save_user_city_record(city_name, country_record)
    else:
        #since the city was found, return the record back 
        return city_query_result    

#method to create a UserReportedCountry record for a given country name 
def create_and_save_user_country_record(self, country_name):
    user_reported_country = UserReportedCountry(name= country_name)
    user_reported_country.put()
    return user_reported_country

#method to create a UserReportedCity record for a given city name and a given country record
def create_and_save_user_city_record (self, city_name, country_record):
    user_reported_city = UserReportedCity(name = city_name, country = country_record)
    user_reported_city.put()
    return user_reported_city

[EDIT#2]

Inside the html form, the call to save the data is done using “post”. Do you think this is still a problem?

<div id="userDataForm">
    <form method="post" action="/UserReporting">
      <p> Select Country: </p>
      <select name="selCountry" id="country">
      <!-- By default, we will select users country -->
      <script type="text/javascript" language="JavaScript">
            document.write("<option value=\"" + geoip_country_name() + "\" selected>"
      </script>
      :
      :
      :
      <p> Select City: </p>
      <div>
        <input type="text" name="city" id="city"> 

        <!-- By default, we will select users city -->
        <script type="text/javascript" language="JavaScript">
            document.getElementById("city").value = geoip_city()
        </script>

      </div>    

      <input type="submit" name="report_down" value="Report Down">
      <input type="submit" name="report_up" value="Report Up"> 
    </form>
<div>           

Initially i tried using the Djangoforms, but i got blocked because i didnt knew how to use javascript to select a value in the djangoform

  • 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-21T02:16:16+00:00Added an answer on May 21, 2026 at 2:16 am

    To address your questions in order:

    1] From the google search, it appears
    “get_or_insert” requires a key, In my
    case in the “UserReportedCountry”
    model, i want the name of the country
    to be the primary key and in the
    “UserReportedCity” model, i want the
    combination of country name + city
    name to be the key. How do i go about
    doing this ?

    Simply specify the name of the country, and the concatenation of country and city (eg “USA/San Francisco” as the key names you pass to get_or_insert. As an aside, get_or_insert is simply syntactic sugar for the following:

    def get_or_insert(cls, key_name, **kwargs):
      def _tx():
        obj = cls.get_by_key_name(key_name)
        if obj is None:
          return cls(key_name, **kwargs)
        else:
          return obj
      return db.run_in_transaction(_tx)
    

    2] Is there a way to use
    “get_or_insert” without specifying a
    key, I came accross the following
    posting on stackoverflow
    (http://stackoverflow.com/questions/4308002/google-app-engine-datastore-get-or-insert-key-name-confusion),
    and tried the idea but it didnt work.

    It doesn’t really make sense to do so. The key is the only unique field for a model in App Engine, and you can’t do cross-entity-group queries in App Engine, so unless you specify one it’s not possible to do a transactional get-or-insert operation. Given your requirements, though, using country name and city name as key names ought to work just fine.

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

Sidebar

Related Questions

A] Problem Summary: I have one to many data models in my project. I
I have create one report and put subreport into summary. I have problem to
I am trying to write documentation comments however I have a problem. /// <summary>
Here's my problem: there's an internal issue tracking system that has a nice summary
You can't use wildcards in menu paths? A quick summary of my problem (which
A] Problem summary: I have JSON data being returned from python to javascript. I
I have two database tables, subscription and transaction , where one subscription can have
I have a very particular problem and after looking at many ressources, i'm unable
I have seen many posts to this problem but no prescribed solution worked for
Problem summary: (i) Widget in static portion of form works fine; but (ii) 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.