How to optimize the code to save data at GAE datastore?
def add_country(country, country_name):
  if country and country_namef and country_name != '':


I assume that country_namef is a typo. If so, there is no point in checking country_name != '' because empty strings are already considered False

    return Country.get_or_insert('i'+str(country), country_name=country_name)
  else:
    return None  

def add_city(city, city_name, country):
  if country and city and city_name and city_name != '':


As before, empty strings are false, so you are checking that twice

    return City.get_or_insert('i'+str(city), city_name=city_name, parent=country)
  else:
    return None