Django如何创建指向同一个模型的多个外键?
2015-05-15 09:41
791 查看
如下所示的House模型,其”province“, "city"和"district"字段类型是指向Region模型的外键。
view
plaincopy
to clipboardprint?
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'....“错误。
原因是指向同一模型的三个外键反向关联名称产生了冲突,Region模型对province的反向关联名称为house_set(),对city的反向关联名称也是house_set(),对district的反向关联名称还是house_set()。
解决方法:给models.ForeignKey()加入related_name参数,定义不同的关联名称。如下代码:
view
plaincopy
to clipboardprint?
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 file<tt literal"="">mysite/models.py.
view
plaincopy
to clipboardprint?
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'....“错误。
原因是指向同一模型的三个外键反向关联名称产生了冲突,Region模型对province的反向关联名称为house_set(),对city的反向关联名称也是house_set(),对district的反向关联名称还是house_set()。
解决方法:给models.ForeignKey()加入related_name参数,定义不同的关联名称。如下代码:
view
plaincopy
to clipboardprint?
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 file<tt literal"="">mysite/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() []
相关文章推荐
- django中一个应用使用另一个应用的模型类并建立外键
- 如何在邮件中创建一个链接指向文件系统中的文件
- 如何创建一个与已知的一个表完全相同结构的新表(包括主键、外键、索引),同时将数据导入新表
- django的数据库模型中的域多次引用同一个外键(表)
- Windows 中如何创建一个指向某相对路径的快捷方式(即快捷方式的目标路径为某相对路径)
- django学习——Model模型中多对多外键如何查询
- 在 MySQL Workbench 中如何可视的建立 MySQL 物理数据模型,并使用它来创建一个 MySQL 数据库
- 如何为系统模型创建一个栏目(图文教程)
- django如何检查创建的模型(model)是否有语法错误或者逻辑错误
- 一个表有两个外键指向另一个表的主键:如何区分显示
- MyBatis中如何实现两个外键指向同一个类
- 在 MySQL Workbench 中如何可视的建立 MySQL 物理数据模型,并使用它来创建一个 MySQL 数据库
- Django(博客系统):基于pycharm如何一个django工程下创建多个app
- 如何计算的字段添加到一个Django模型
- Unix/Linux如何创建一个后台进程(run background process)
- 如何创建一个成功的数据仓库(datawarehouse)(想了解数据仓库的人士快看)
- Android入门-新手如何成功创建一个Android小应用
- 为你的机器学习模型创建一个API服务
- [django]如何在 list_filter 中包含外键字段
- 如何创建一个用弹出窗口来查看详细信息的超链接列