1👍
Something like:
qs = Model2.objects.filter(model3__flag=True).select_related('model1').distinct()
for m2 in qs:
print m2.model1.name, m2.name
# only fetch the names,
# this works if you treat duplicated ('name', 'model1__name') tuples same and show them once.
qs = qs.values('model1__name', 'name')
for x in qs:
print x['model1__name'], x['name']
👤okm
Source:stackexchange.com