ContentType组件

ContentType组件

ContentType是Django的内置的一个应用,可以追踪项目中所有的APP和model的对应关系,并记录在ContentType表中。

当我们的项目做数据迁移后,会有很多django自带的表,其中就有django_content_type表,我们可以去看下~~~

ContentType组件应用:

  -- 在model中定义ForeignKey字段,并关联到ContentType表,通常这个字段命名为content-type

  -- 在model中定义PositiveIntergerField字段, 用来存储关联表中的主键,通常我们用object_id

  -- 在model中定义GenericForeignKey字段,传入上面两个字段的名字

  --  方便反向查询可以定义GenericRelation字段

原文地址:https://www.cnblogs.com/hnlmy/p/9703699.html