Djangoは、同じモデルを指す複数の外部キーをどのように作成しますか?
3915 ワード
以下に示すHouseモデル、その「province」、「city」および「district」フィールドタイプは、Regionモデルを指す外部キーです.
view plain copy to clipboard print ?
class Region(models.Model):
「」「行政区域表国、省、市、区(県) """
parent = models.ForeignKey('self')
name = models.CharField(max_length=30)
region_type = models.IntegerField()
class Meta:
db_table = "regions"
class House(models.Model):
」「家屋表 """
province = models.ForeignKey(Region)
city = models.ForeignKey(Region)
district = models.ForeignKey(Region)
address = models.CharField(max_length=200)
name = models.CharField(max_length=200)
longitude = models.FloatField()#経度 latitude = models.FloatField()#緯度 created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True, null=True)
表面的には問題ないように見えますがsyncdbを行うと「house.house: Accessor for field 'province' clashes with related field 'Region.house_set'. Add a related_name argument to the definition for 'province'....「エラー.
同じモデルを指す3つの外部キーの逆関連名が競合しているため、Regionモデルのprovinceに対する逆関連名はhouse_set()は、cityに対する逆関連名もhouse_set()、districtに対する逆関連名かhouse_set().
解決方法:models.ForeignKey()related_に参加nameパラメータで、異なる関連名を定義します.次のコードがあります.
view plain copy to clipboard print ?
class House(models.Model):
」「家屋表 """
province = models.ForeignKey(Region, related_name='province_houses')
city = models.ForeignKey(Region, related_name='city_houses')
district = models.ForeignKey(Region, related_name='district_houses')
address = models.CharField(max_length=200)
name = models.CharField(max_length=200)
longitude = models.FloatField()#経度 latitude = models.FloatField()#緯度 created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True, null=True)
参考記事:Relating a model to another model more than once
This sample code assumes the above model has been saved in a filemysite/models.py.
view plain copy to clipboard print ?
class Region(models.Model):
「」「行政区域表国、省、市、区(県) """
parent = models.ForeignKey('self')
name = models.CharField(max_length=30)
region_type = models.IntegerField()
class Meta:
db_table = "regions"
class House(models.Model):
」「家屋表 """
province = models.ForeignKey(Region)
city = models.ForeignKey(Region)
district = models.ForeignKey(Region)
address = models.CharField(max_length=200)
name = models.CharField(max_length=200)
longitude = models.FloatField()#経度 latitude = models.FloatField()#緯度 created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True, null=True)
表面的には問題ないように見えますがsyncdbを行うと「house.house: Accessor for field 'province' clashes with related field 'Region.house_set'. Add a related_name argument to the definition for 'province'....「エラー.
同じモデルを指す3つの外部キーの逆関連名が競合しているため、Regionモデルのprovinceに対する逆関連名はhouse_set()は、cityに対する逆関連名もhouse_set()、districtに対する逆関連名かhouse_set().
解決方法:models.ForeignKey()related_に参加nameパラメータで、異なる関連名を定義します.次のコードがあります.
view plain copy to clipboard print ?
class House(models.Model):
」「家屋表 """
province = models.ForeignKey(Region, related_name='province_houses')
city = models.ForeignKey(Region, related_name='city_houses')
district = models.ForeignKey(Region, related_name='district_houses')
address = models.CharField(max_length=200)
name = models.CharField(max_length=200)
longitude = models.FloatField()#経度 latitude = models.FloatField()#緯度 created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True, null=True)
参考記事:Relating a model to another model more than once
Model source code
from django.db import models
class Person(models.Model):
full_name = models.CharField(max_length=20)
mother = models.ForeignKey('self', null=True, related_name='mothers_child_set')
father = models.ForeignKey('self', null=True, related_name='fathers_child_set')
def __unicode__(self):
return self.full_name
Sample API usage
This sample code assumes the above model has been saved in a filemysite/models.py.
>>> from mysite.models import Person
# Create two Person objects -- the mom and dad in our family.
>>> dad = Person(full_name='John Smith Senior', mother=None, father=None)
>>> dad.save()
>>> mom = Person(full_name='Jane Smith', mother=None, father=None)
>>> mom.save()
# Give mom and dad a kid.
>>> kid = Person(full_name='John Smith Junior', mother=mom, father=dad)
>>> kid.save()
>>> kid.mother
<Person: Jane Smith>
>>> kid.father
<Person: John Smith Senior>
>>> dad.fathers_child_set.all()
[<Person: John Smith Junior>]
>>> mom.mothers_child_set.all()
[<Person: John Smith Junior>]
>>> kid.mothers_child_set.all()
[]
>>> kid.fathers_child_set.all()
[]