作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我根据文档编写了此类,以便能够对应用程序中具有 id 的任何内容进行投票:
class Vote(models.Model):
class Meta:
unique_together = ('voted_id', 'voter_id', 'content_type', 'vote_type')
voted_id = models.PositiveIntegerField()
vote_type = models.BooleanField(null=True)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
voter_id = models.PositiveIntegerField()
content_object = GenericForeignKey('content_type', 'voter_id')
def __str__(self):
return self.content_object
然后在数据库表中,我有 5 列:
id, voted_id, vote_type, voter_id, content_type_id
我不太明白 content_type_id 指的是什么:它是虚拟 ID 吗?
因为在我的理解中,当我写道:
from forum.models import User, Vote
kdelanyd = User.objects.get(username='kdelanyd')
v = Vote(content_object=kdelanyd, voted_id=1, vote_type=False)
v.save()
我认为 content_type 持有“kdelanyd”引用,然后,有点它的 id:它没有。
最佳答案
在你的数据库中可能有表是 django_content_type
。而content_type
、content_type_id
、content_object
在这个表中引用。它用于定义诸如数据
的“类型”之类的任何内容。
像轮子
,它可能是汽车
或自行车
的轮子。在这种情况下,car
和 bicycle
是一个 content_object
。 django_content_type
表中car
和bicycle
的id为content_type_id
关于 Django : content_type_id in GenericForeignKey,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53840047/
我根据文档编写了此类,以便能够对应用程序中具有 id 的任何内容进行投票: class Vote(models.Model): class Meta: unique_toget
我是一名优秀的程序员,十分优秀!