On 5/5/2009 12:47 AM, bweiss wrote:
> Thanks Alex, that's really helpful and I'm certainly closer now!
> However, after a fair bit of experimenting with that exclude() method,
> I still can't quite get it to do what I need.
>
> I'm trying to get a list of all employees who do not have a
> qualification of a certain type (which I'llcall 'A1').
>
> I can get a list of those who do have the qualification by using a
> lookup on the related "Qualification" model as follows:
> Employee.objects.filter(qualification__type__exact='A1')
>
> Essentially, what I need is a list of all Employees, minus the names
> on that filtered list.
>
> If I use the exclude() method on the same lookup (so,
> Employee.objects.exclude(qualification__type__exact='A1')), the query
> returns those employees who have a qualification of any type other
> than A1. It doesn't include the employees who have no qualifications
> at all, and it *does* include the ones who have A1 as well as other
> qualifications.
>
> Can anyone suggest what I'm doing wrong?
{{{
excludes = Employee.objects.filter(
qualification__type__exact='A1').values_list('pk', flat=True)
what_you_want = Employee.objects.exclude(pk__in=excludes)
}}}
or
{{{
all = Employee.objects.all()
excludes = Employee.objects.filter(qualification__type__exact='A1')
what_you_want = set(all) - set(excludes)
}}}
--
George
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---