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 に答える