3👍
Use the fact that Django allows nested querying:
def Foo(request):
parent_ids=Parent.objects.values('id')
orphans=Children.objects.exclude(parent_id__in=parent_ids)
return render_to_response('Foo.html', {'orphans':orphans})
1👍
No answers yet so here’s my attempt to answer my own question
Here is an ugly solution that works…
def Foo(request):
orphans=[]
for child in Children.objects.all():
try:
if child.parent:
pass
except Parent.DoesNotExist:
orphans.append(child)
return render_to_response('Foo.html',{'orphans':orphans})
Hopefully there is a better way?
Update improved method…
def Foo(request):
parent_id_list=[row.pk for row in Parent.objects.all()]
orphans=Children.objects.exclude(pk__in=parent_id_list)
return render_to_response('Foo.html',{'orphans':orphans})
Source:stackexchange.com