> I have two models: > > Child(models.Model): > name = CharField > .... > > Sponsorship(models.Model): > name = CharField > child = ForeignKey(Child) > ... > > how do i get a list of all children who are not in Sponsorship?
AFAIK, for any sort of efficiency, you have to twiddle the underlying query using a call to extra() http://www.djangoproject.com/documentation/db_api/#extra-select-none-where-none-params-none-tables-none which would look something like unsponsored = Child.objects.extra( where=[""" id not in ( SELECT child_id FROM app_sponsorship ) """] ) This should work in any standards-compliant SQL...Older versions of MySQL choked on this, but newer versions should handle it just fine. This can even be expanded in case you want to see something like this imagined scenario (assuming your Sponsorship model has a "is_corporation" bit-flag to determine whether sponsorship was personal or corporate): unsponsored = Child.objects.extra( where=[""" id not in ( SELECT child_id FROM app_sponsorship WHERE is_corporation = %s ) """], params=[True] ) Which would return Child objects that don't have corporate sponsorship. HTH, -tkc --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---