Django中的contenttypes框架
使用django-admin startproject {项目名}后,
# settings.py
DJANGO_APPS = [
...
"django.contrib.contenttypes",
...
]
并且在生成数据库时,会默认生成一张django_content_type表,如下所示
这张表记录了所有 模型类名字 与 所属的应用
id | app_label | model |
---|---|---|
1 | 应用名 | 模型类 类名 |
2 | … | … |
3 | … | … |
来看一下此模型类的 源码(重要部分):
from django.contrib.contenttypes.models import ContentType
class ContentType(models.Model):
# 应用名
app_label = models.CharField(max_length=100)
# 模型类名
model = models.CharField(_('python model class name'), max_length=100)
# 自定义的 模型类的管理器
objects = ContentTypeManager()
# 元数据
class Meta:
verbose_name = _('content type')
verbose_name_plural = _('content types')
db_table = 'django_content_type'
unique_together = (('app_label', 'model'),)
def __str__(self):
return self.name
@property # 将函数装华为属性
def name(self):
'''获取 指定self.app_label, self.model 模型类的 类名'''
model = self.model_class()
if not model:
return self.model
# 获取到 model 则将 model元数据的 verbose_name 返回
return str(model._meta.verbose_name)
def model_class(self):
"""Return the model class for this type of content."""
try:
# django.apps 模块的public的方法,返回与app_label, model对应的模型类
return apps.get_model(self.app_label, self.model)
except LookupError:
return None
PS: 在Python代码中,可以使用django.apps.apps引用上述settings.py中的INSTALLED_APPS变量。django.apps.apps也被称为应用注册器
综上所述可以概括为:
ContentType 是由Djnago框架提供的一个核心功能,对当前项目中所有基于Django驱动的model(继承自models.Model并且写在modles.py中)提供了更高层次的model接口
那么生成这张表有什么作用呢?
-
Django权限管理中的Permission借助ContentType 实现了对任意models的权限操作
-
ContentType的通用类型 - GenericRelation
ContentType的通用类型 - GenericRelation
什么是GenericRelation和GenericForeignKey
假设现在有一个 博客项目 开发模型类时,有文章、图片等等 都需要可评论(comment)
简单代码如下:
from django.db import models
from django.contrib.auth.models import User
# 博客
class Post(models.Model):
author = models.ForeignKey(User,on_deleter=models.CASCADE)
body = models.TextField(blabk=True)
# 文章
class Articles(models.Model):
author = models.ForeignKey(User,on_deleter=models.CASCADE)
body = models.TextField(blabk=True)
# 图片
class Pirture(models.Model):
author = models.ForeignKey(User,on_deleter=models.CASCADE)
body = models.TextField(blabk=True)
# 评论
class Comment(models.Model):
author = models.ForeignKey(User,on_deleter=models.CASCADE)
body = models.TextField(blabk=True)
# 一一关联到外键 这种定义,任意生效其他处需要设置为空 只有一个字段有值(评论post时,pic和article为空),
post = models.ForeignKey(Post,on_deleter=models.CASCADE,null=True)
pic = models.ForeignKey(Pirture,on_deleter=models.CASCADE,null=True)
article = models.ForeignKey(Articles,on_deleter=models.CASCADE,null=True)
此时如果增加模型类如视频等,Comment模型类中又要增加外键字段 -> 扩展性差且无法做到模型类中每一字段都有意义
如果将每一种评论都单独分离出来,变为一个个模型类,这样做解决了模型类中每一字段都有意义的问题,但同样扩展性很差
使用ContentType 将Comment模型类变为通用模型类
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes.fields import GenericForeignKey
# 要找到一条表记录,需要模型类名,模型名,和主键id名
# 而刚好content_type 都可以实现
# 将Comment 变为通用模型类
class Comment(models.Model):
author = models.ForeignKey(User,on_delete=models.CASCADE)
body = models.TextField(blabk=True)
# 外键关联到ContentType,获得app_lable ,model 数据
content_type = models.ForeignKey(ContentType,models.CASCADE)
# 获取主键id - 其他表的主键id 要考虑类型
object_id = models.CharField()
# "content_type","object_id" 可省略
# !!注意:GenericForeignKey 默认为删除级联,但不支持on_delete参数
content_object = GenericForeignKey("content_type","object_id")
# 之后再文章、图片等模型类中添加comments = GenericRelation()
from django.contrib.contenttypes.fields import GenericRelation
class Post(models.Model):
author = models.ForeignKey(User,on_deleter=models.CASCADE)
body = models.TextField(blabk=True)
# 关联到Comment 模型类
comments = GenericRelation(Comment)
class Articles(models.Model):
author = models.ForeignKey(User,on_deleter=models.CASCADE)
body = models.TextField(blabk=True)
comments = GenericRelation(Comment)
class Pirture(models.Model):
author = models.ForeignKey(User,on_deleter=models.CASCADE)
body = models.TextField(blabk=True)
comments = GenericRelation(Comment)
# 总结:
# 想让那张表变为通用模型类类
# 在模型类中定义
content_type = models.ForeignKey(ContentType,models.CASCADE)
object_id = models.IntegerField()
content_object = GenericForeignKey("content_type","object_id")
# 再让其模型类使用 GenericRelation 关联到 通用模型类类
字段名 = GenericRelation(通用模型类名)
来看官方文档说明:
https://docs.djangoproject.com/en/2.2/ref/contrib/contenttypes/
并用于 ContentType启用模型之间的真正通用(有时称为“多态”)关系。
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models
class TaggedItem(models.Model):
tag = models.SlugField()
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
object_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'object_id')
def __str__(self):
return self.tag
一个常见的ForeignKey只能“指向”另一个模型,这意味着如果该TaggedItem模型使用ForeignKey, 则必须选择有且只有一个模型来存储标签。contenttypes应用程序提供了一个特殊的字段类型(GenericForeignKey),可以解决该问题,并允许与任何模型建立关系:
类GenericForeignKey
设置三部分 GenericForeignKey:
-
将您的模型ForeignKey 设为ContentType。该字段的常用名称是“content_type”。
-
给您的模型设置一个字段,该字段可以存储您将要关联的模型中的主键值。对于大多数模型,这意味着 PositiveIntegerField。该字段的常用名称是“ object_id”。
- PositiveIntegerField,但仅允许在特定点(与数据库有关)下的值。从0到的值32767在Django支持的所有数据库中都是安全的。
-
给您的模型一个 GenericForeignKey,并向其传递上述两个字段的名称。如果将这些字段分别命名为“ content_type”和“ object_id”,则可以忽略这些-这些是默认字段名称 GenericForeignKey。
-
for_concrete_model
如果为False,则该字段将能够引用代理模型。默认值为True。这将for_concrete_model论点反映到 get_for_model() -
主键类型兼容性
“ object_id”字段不必与相关模型上的主键字段具有相同的类型,但通过其get_db_prep_value()方法,其主键值必须可强制为与“ object_id”字段相同的类型 。
例如,如果要允许具有主键字段IntegerField或 CharField主键字段的模型的通用关系 ,则可以将其CharField
用于模型上的“ object_id”字段,因为可以将整数强制为get_db_prep_value()。
总结: 推荐使用CharField 作为object_id 字段类型
为了获得最大的灵活性,您可以使用 TextField未定义最大长度的,但这可能会导致严重的性能损失,具体取决于您的数据库后端。
没有最适合领域类型的“一刀切”解决方案。您应该评估预期要指向的模型,并确定哪种解决方案对您的用例最有效。
与正常使用的API类似的API ForeignKey; 每个对象TaggedItem都有一个content_object返回与其相关的对象的字段,您也可以分配给该字段或在创建时使用TaggedItem:
# 导入User模型类
>>> from django.contrib.auth.models import User
# 在User表中创建一个用户 username='Guido' 返回查询集 并赋值给变量guido
>>> guido = User.objects.get(username='Guido')
# 创建TaggedItem 并将content_object关联为guido
>>> t = TaggedItem(content_object=guido, tag='bdfl')
# 数据库保存
>>> t.save()
# 通过外键查询 关联的数据
>>> t.content_object
<User: Guido>
如果删除了相关对象,则content_type和object_id字段将保持设置为原始值,并GenericForeignKey返回 None:
>>> guido.delete()
>>> t.content_object # returns None
由于GenericForeignKey ,不能直接使用的过滤器(filter() 以及exclude() 等database API)。由于 GenericForeignKey是一个不常见的文件对象,这些例子将不工作:
# This will fail
>>> TaggedItem.objects.filter(content_object=guido)
# This will also fail
>>> TaggedItem.objects.get(content_object=guido)
逆向通用关联
class GenericRelation
related_query_name
默认情况下,不存在相关对象与该对象之间的关系。设置related_query_name会创建一个从相关对象到该对象的关系。这允许从相关对象进行查询和过滤。
如果您知道最常使用哪种模型,则还可以添加“反向”通用关系以启用其他API。例如:
from django.contrib.contenttypes.fields import GenericRelation
from django.db import models
class Bookmark(models.Model):
url = models.URLField()
tags = GenericRelation(TaggedItem)
Bookmark每个实例将具有一个tags属性,可用于检索其关联的TaggedItems:
>>> b = Bookmark(url='https://www.djangoproject.com/')
>>> b.save()
>>> t1 = TaggedItem(content_object=b, tag='django')
>>> t1.save()
>>> t2 = TaggedItem(content_object=b, tag='python')
>>> t2.save()
>>> b.tags.all()
<QuerySet [<TaggedItem: django>, <TaggedItem: python>]>
定义GenericRelation用 related_query_nameset 允许从相关对象中查询:
tags = GenericRelation(TaggedItem, related_query_name='bookmark')
使用TaggedItem filtering, ordering,或者其他查询对bookmark 进行操作
>>> # Get all tags belonging to bookmarks containing `django` in the url
>>> TaggedItem.objects.filter(bookmark__url__contains='django')
<QuerySet [<TaggedItem: django>, <TaggedItem: python>]>
当然,如果您不添加related_query_name,则可以手动执行相同类型的查找:
>>> bookmarks = Bookmark.objects.filter(url__contains='django')
>>> bookmark_type = ContentType.objects.get_for_model(Bookmark)
>>> TaggedItem.objects.filter(content_type__pk=bookmark_type.id, object_id__in=bookmarks)
<QuerySet [<TaggedItem: django>, <TaggedItem: python>]>
正如GenericForeignKey 接受content-type和object-ID字段的名称作为参数一样, 如果具有通用外键的模型为这些字段使用非默认名称,则必须在GenericRelation为其设置时传递字段名称 。例如,如果TaggedItem上面提到的模型使用了名为的字段content_type_fk并 object_primary_key创建其通用外键,则GenericRelation需要像这样定义它:
tags = GenericRelation(
TaggedItem,
content_type_field='content_type_fk',
object_id_field='object_primary_key',
)
另请注意,如果删除具有的对象,则 指向GenericRelation该对象的所有对象GenericForeignKey也将被删除。在上面的示例中,这意味着如果Bookmark删除了一个对象,则TaggedItem指向该对象的所有对象将同时被删除。
与ForeignKey, GenericForeignKey不同,它不接受on_delete自定义此行为的参数。如果需要,您可以通过不使用来避免级联删除 GenericRelation,并且可以通过pre_delete 信号提供替代行为。
蹩脚的翻译,如妨碍阅读请移步官方文档:https://docs.djangoproject.com/en/2.2/ref/contrib/contenttypes/
!!!