[Answer]-Using Generic Foreign Keys to retrieve data

1👍

✅

OK. I see what you’re doing. First, there is no way to do what you are trying to do using the content_types API. But it can be done.

As you stated:

x = Streams.objects.all() # all stream objects
y = x[0].content_type # CommentData ContentType object

So you need to use basic Django ORM like this:

from django.db.models.loading import get_model

x = Streams.objects.all() # all stream objects
y = x[0].content_type # CommentData ContentType object
model_class = get_model(app_label=y.app_label, model_name=y.model)

model_class.objects.all() # all objects of the type

Leave a comment