Given the following model:

class ModelA(models.Model):
  name = models.CharField(max_length=10)

class ModelB(models.Model):
  a = models.ForeignKey(ModelA)
  name = models.CharField(max_length=10)

I'd like to perform a query for ModelA objects that have no ModelB
objects referring to them.  I've tried the following:

qs = ModelA.objects.filter(modelb=None)
qs = ModelA.objects.filter(modelb__isnull=True)

but these don't work because INNER JOINs are performed on the tables.
The following works but is kind of kludgey:

qs = ModelA.objects.exclude(pk__in=[b.a._get_pk_val() for b in
ModelB.objects.all() if b.a is not None])

Is there currently a better way to do this?  If not, perhaps something
like the syntax above could be added to query.py?  It could be
implemented by using a LEFT JOIN and checking for NULL in the ModelB.id
column.

Thoughts?

Regards,
Casey



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to