Django的复合外键支持
django-composite-foreignke的Python项目详细描述
允许创建一个django foreignkey,它不与其他模型的pk链接,而是与本地模型列或固定值匹配的多列。
有些数据库有一个复合主键,这导致django外键不可能被使用。
现在,django不支持复合主键see ticket,而foreignkey不支持多列。 但幸运的是,foreignkey的基类很好地支持它,所以这个库只是在foreignobject周围添加了一个小包装器,使它更有用。 这个实现的真正附加功能是支持使用原始值定制链接。
< P>复合外键的实现通过强制提供另一个模型的对应列,而不是有力的主键来跳过复合主键的复杂度。安装
使用pip安装:
pip install django-composite-foreignkey
或者,您可以安装下载或克隆此repo并调用
^{tt2}$.
示例
您有这个型号
classCustomer(models.Model):company=models.IntegerField()customer_id=models.IntegerField()name=models.CharField(max_length=255)address=CompositeForeignKey(Address,on_delete=CASCADE,to_fields={"tiers_id":"customer_id","company":LocalFieldValue("company"),"type_tiers":RawFieldValue("C")})classMeta(object):unique_together=[("company","customer_id"),]classContact(models.Model):company_code=models.IntegerField()customer_code=models.IntegerField()surname=models.CharField(max_length=255)# virtual fieldcustomer=CompositeForeignKey(Customer,on_delete=CASCADE,related_name='contacts',to_fields={"customer_id":"customer_code","company":"company_code"})
您可以像使用任何外键一样使用contact.customer,但在场景中,它将使用公司和客户ID查询客户表。
要求
- python 2.7、3.4、3.5、3.6、3.7
- Django 1.11、2.0、2.1
欢迎对其他django和python版本的贡献和pull请求。
错误和请求
如果您发现了一个bug或者有其他功能的请求,请使用github上的问题跟踪器。
https://github.com/onysos/django-composite-foreignkey/issues
许可证
你可以在gplv3下使用这个。
谢谢
感谢django提供了这个令人惊叹的框架。感谢django-bootstrap3的应用程序结构。