If I have a django content_type reference (the id of the model.class and the id of the object), what's the best way to get the actual object itself?
如果我有一个django content_type引用(model.class的id和对象的id),那么获取实际对象本身的最佳方法是什么?
Sounds trivial but I can't actually see an example anywhere.
听起来微不足道,但我实际上无法在任何地方看到一个例子。
1 个解决方案
#1
38
From memory, it is something like this:
从记忆中,它是这样的:
from django.contrib.contenttypes.models import ContentType
ct = ContentType.objects.get_for_id(content_type)
obj = ct.get_object_for_this_type(pk=object_id)
#1
38
From memory, it is something like this:
从记忆中,它是这样的:
from django.contrib.contenttypes.models import ContentType
ct = ContentType.objects.get_for_id(content_type)
obj = ct.get_object_for_this_type(pk=object_id)