djangodjango-contenttypes

django: How to get objects from a ContentType instance


I want to get a queryset of objects from a ContentType instance and then be able to filter them. From the doc, it is possible to get() an object using:

ct.get_object_for_this_type(**kwargs)

How can I make a similar filter() for that instance?


Solution

  • Since you have the ContentType instance you can do ct.model_class() to get the model class and then use it as you normally would.

    model_class = ct.model_class()
    model_class.objects.filter(**kwargs)