Python 构建与家长相关的模型布局?

Python 构建与家长相关的模型布局?,python,google-app-engine,django-models,Python,Google App Engine,Django Models,如何有效地构建数据库模型 例如,假设我有一个国家模型,其属性类似于“名称、北半球(布尔值)、人口、州(州列表)、首都(布尔值)” 还有另一种模式,称为州或县或其他有财产“名称、人口、城市(城市列表)”的模式 还有另一种称为“城市”的模型,其属性为“名称、首都(布尔值)、与首都的距离、人口” 显然,我需要城市来存储与某些州相关的数据,因此州需要与特定国家相关的数据。在我的州模型中,我会有加利福尼亚州、科罗拉多州等,每个州都必须有一个特定的城市列表 一个人如何构造他的模型,使它们以某种方式相互关联?

如何有效地构建数据库模型

例如,假设我有一个国家模型,其属性类似于“名称、北半球(布尔值)、人口、州(州列表)、首都(布尔值)”

还有另一种模式,称为州或县或其他有财产“名称、人口、城市(城市列表)”的模式

还有另一种称为“城市”的模型,其属性为“名称、首都(布尔值)、与首都的距离、人口”

显然,我需要城市来存储与某些州相关的数据,因此州需要与特定国家相关的数据。在我的州模型中,我会有加利福尼亚州、科罗拉多州等,每个州都必须有一个特定的城市列表


一个人如何构造他的模型,使它们以某种方式相互关联?我对MVC非常陌生,所以在概念上很难理解。是否可以使用类(父)构造函数?

如果你想在Google App Engine的数据存储中存储关系数据,这是一篇很好的文章,可以从以下开始:

用于指定两个模型之间的关系:

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

class State(db.Model):
    country = db.ReferenceProperty(Country, collection_name='states')
    name = db.StringProperty(required=True)

class City(db.Model):
    state = db.ReferenceProperty(State, collection_name='cities')
    name = db.StringProperty(required=True)
您的
Country
模型的实例将自动获取一个名为
states
的新属性,该属性将是一个查询,用于获取所有相关的
State
实体。与城市的
State
模型相同。其自动创建的
cities
属性将是一个查询,用于获取所有相关的
City
e20多岁

如何使用:

# Create a new country:
us = Country(name='USA')
us.put()

# Create a new state
ca = State(name='California', country=us)
ca.put()

# Create a new city
la = City(name='Los Angeles', state=ca)
la.put()

# And another
sf = City(name='San Francisco', state=ca)
sf.put()

# Print states
for state in us.states:
    print state.name

    # Print cities
    for city in state.cities:
        print ' ' + city.name
应输出:

California
 Los Angeles
 San Francisco

如果您想将关系数据存储在Google App Engine的数据存储中,这是一篇很好的文章

用于指定两个模型之间的关系:

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

class State(db.Model):
    country = db.ReferenceProperty(Country, collection_name='states')
    name = db.StringProperty(required=True)

class City(db.Model):
    state = db.ReferenceProperty(State, collection_name='cities')
    name = db.StringProperty(required=True)
您的
Country
模型的实例将自动获取一个名为
states
的新属性,该属性将是一个查询,用于获取所有相关的
State
实体。与城市的
State
模型相同。其自动创建的
cities
属性将是一个查询,用于获取所有相关的
City
e20多岁

如何使用:

# Create a new country:
us = Country(name='USA')
us.put()

# Create a new state
ca = State(name='California', country=us)
ca.put()

# Create a new city
la = City(name='Los Angeles', state=ca)
la.put()

# And another
sf = City(name='San Francisco', state=ca)
sf.put()

# Print states
for state in us.states:
    print state.name

    # Print cities
    for city in state.cities:
        print ' ' + city.name
应输出:

California
 Los Angeles
 San Francisco

谢谢你的快速回复。请你稍微详细说明一下。我的意思是,我不想让他们继承atm机上的财产。啊,对不起,我改变了我的答案。酷,这看起来正是我需要的-我会调查一下。我会暂时不回答这个问题。啊,我明白了。所以如果我要使用“State.cities.population”之类的词“它将返回该特定实体的填充?不,首先,该属性仅存在于模型实例上。因此,您必须首先获得一个
状态
实例。在这种情况下,您可以访问如下城市:
for city in state.cities:print city.population
感谢您的快速回复。你能稍微详细说明一下吗。我的意思是在关系上-我不希望他们继承atm机上的财产。啊,对不起,我改变了我的答案。酷,这看起来正是我需要的-我会调查的。我暂时不回答这个问题。啊,我明白了。因此,如果我使用类似“State.cities.population”的东西,它将返回该特定实体的人口?不,首先,该属性只存在于模型实例上。因此,您必须首先获得一个
状态
实例。在这种情况下,您可以像这样访问城市:
for city in state.cities:print city.population