4

如何建模以下关系:

class Country(models.Model):
  # The capital city of this country
  capital = models.ForeignKey(City)
  ## other country stuff

class City(models.Model):
  # The country where this city lies in
  country = models.ForeignKey(Country)
  ## other city stuff

这显然不能编译。(城市在国家的定义中未定义)。有什么建议么?

4

1 回答 1

3

您可以使用字符串而不是模型类来引用模型:

class Country(models.Model):
  # The capital city of this country
  capital = models.ForeignKey('City', related_name='+')
  ## other country stuff

另见:

于 2013-09-23T19:09:19.767 回答