On Fri, 2008-08-29 at 10:51 -0700, ryan wrote:
> I have 2 querysets q1 and q2.
> 
> I want q3 = q2-q1, where q3 is a queryset object and contains only the
> objects in q2 that are not in q1.
> 
> Is there built in support for this operation?
> 
> The only thing I can think to do is:
> list( set(q2) - set(q1) )
> It doesn't return a queryset, but I can deal with it if there's no
> better alternative.

If they already exist as querysets, the set difference is the way to go.
There's probably no need to turn it back into a list at the end, since
Python's sets are iterable containers. It depends what you want to do
with it afterwards.

However, if you know the filters you are applying, you can get the
difference by changing all the filter() calls in the second queryset to
exclude() calls. So if you can hook into the process a bit earlier, you
might be able to generate a more effective SQL query.

Regards,
Malcolm



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" 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-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to