2009-08-03 31 views
13

如果我有一個django content_type引用(model.class的id和對象的id),獲取實際對象本身的最佳方式是什麼?Django content-type:如何獲取對象?

聲音微不足道,但我實際上無法在任何地方看到例子。

+1

你想[`get_object_for_this_type`](http://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#django .contrib.contenttypes.models.ContentType.get_object_for_this_type)? – 2009-08-03 08:45:42

回答

36

從內存中,它是這樣的:

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) 
+4

你先生,有一個很好的記憶:) – 2012-05-10 07:52:05