interstar
interstar

Reputation: 27196

Django content-type : how do I get an object?

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?

Sounds trivial but I can't actually see an example anywhere.

Upvotes: 21

Views: 12115

Answers (1)

Wogan
Wogan

Reputation: 72717

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)

Upvotes: 56

Related Questions