Generict Django content type,获取contentyp中存储的对象

2024-05-19 17:07:34 发布

您现在位置:Python中文网/ 问答频道 /正文

有人问过类似的问题,但没有一个对我有用

下面是我的contentype模型

class notifyall(models.Model):     
    target_instance=models.ForeignKey(ContentType,null=True,on_delete=models.CASCADE,blank=True)
    object_id=models.PositiveIntegerField(null=True)
    target_object=GenericForeignKey('target_instance', 'object_id')

如您所见,contentype有一个GenericForeignKey来存储来自不同模型的不同类型的对象

当notifyall对象被创建时,这就是我将得到的

>>object_id= e.g 20
>>target_instance = class_name_of_model

检索notifyall模型中存储的对象作为object\u id和target\u instance的正确方法是什么

请给我你的答案或建议的代码库


Tags: 对象instance模型idtruetargetmodelobject