2011-10-26 43 views
0

我有以下代码(CountryCitykey_name是数字ID与另外的“我”开头):如何优化向数据存储添加数据的代码?

def add_country(country, country_name): 
    if country and country_name and country_name != '': 
    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 != '': 
    return City.get_or_insert('i'+str(city), city_name=city_name, parent=country) 
    else: 
    return None 

它是正确的代码还是可以以某种方式优化?

+2

实际上,这种问题会更好http://codereview.stackexchange.com/ – JMax

+0

@JMax,codereview上没有与GAE相关的标签... –

+0

您可以创建一些并至少用'Python'存在 – JMax

回答

0

你不需要在你的ID前添加一个字符 - 只需将它们转换为一个字符串并按原样使用即可。否则,这可能是好的,但是如果不知道如何使用它以及如何获取对象,很难知道。

相关问题