content_type

1、content_type:https://blog.csdn.net/Ayhan_huang/article/details/78626957

from django.contrib.contenttypes.models import ContentType

from django.contrib.contenttypes.fields import GenericForeignKey

通过使用contenttypes 应用中提供的特殊字段GenericForeignKey,我们可以很好的解决这个问题。只需要以下三步:

  • 在model中定义ForeignKey字段,并关联到ContentType表。通常这个字段命名为“content_type”
  • 在model中定义PositiveIntegerField字段,用来存储关联表中的主键。通常这个字段命名为“object_id”
  • 在model中定义GenericForeignKey字段,传入上述两个字段的名字。

为了更方便查询,我们还可以在外键字段关联的类中通过GenericRelation字段定义反向关系

原文地址:https://www.cnblogs.com/Ming-Hui/p/9306139.html