Django inner join to a model without directly relationship
Solution 1:
Try this:
model.BazModel.objects.all().select_related('bar').prefetch_related('bar__foomodel_set')
And then in BazSerializer
, you can set the source
for foo
like this:
class BazSerializer(serializer.ModelSerializer):
foo = FooSerializer(source='bar.foomodel_set')
This will tell the foo
serializer to get it's data from the FooModel
objects using BazModel
's bar
.
You might have to change foomodel_set
to the related name you have set to access FooModel
from BarModel
.