django的contenttype表

https://blog.csdn.net/aaronthon/article/details/81714496

这篇文章已经非常详细了,供自己以后忘了...回看......

 总结:  

当一张表和多个表FK关联,并且多个FK中只能选择其中一个或其中n个时,可以利用contenttype,只需定义三个字段就搞定!(固定)

content_type = models.ForeignKey(ContentType,on_delete=models.CASCADE)  # 关联表
object_id = models.PositiveIntegerField() # 关联表的对象的pk                  
content_object = GenericForeignKey('content_type', 'object_id') # 不会生成表字段,用于正向查询关联对象 obj.content_object

  

price_policy = GenericRelation("PricePolicy")  # GenericForeignKey反向查询,不会生成表字段  obj.price_policy.all()

  

猜你喜欢

转载自www.cnblogs.com/amber-liu/p/10120723.html