I found a solution from the annotate docs: A.objects.values('b__name').distinct()
tada! hope this helps someone else later :) On Jan 10, 3:43 pm, John <theu...@gmail.com> wrote: > I'm trying to express this query using the Django ORM: > > SELECT DISTINCT test_b.name FROM test_a LEFT JOIN test_b ON > test_a.b_id = test_b.id > > using Django (with models 'A' and 'B') all I seem to be able to get is > > SELECT DISTINCT test_a.name, test_a.id FROM a INNER JOIN test_b ON > test_a.b_id = test_b.id > > via the query: > > A.objects.all().distinct('b__name') > > I've tried various changes, including select_related() to try and > convince it to follow the relation, but no matter what I do it ends up > using the fields from the A model in the DISTINCT clause rather than > B. I assume that part of the problem is that the query I want to > express does not return an A object, but I can't think of how to use > the B model to phrase it in the ORM > (B.objects.all.distinct('name').filter( "has at least one A > model" ) ? ) -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.