I had been hunting around for some way to do negation in queries, and
found QNot, though it is undocumented and not exported with Q. I
though this was a little bit clumsy, so here's a patch to add unary
negation to Q objects:

Index: django/db/models/query.py
===================================================================
--- django/db/models/query.py   (revision 3656)
+++ django/db/models/query.py   (working copy)
@@ -618,6 +618,9 @@
     def __or__(self, other):
         return QOr(self, other)

+    def __neg__(self):
+        return QNot(self)
+
     def get_sql(self, opts):
         return parse_lookup(self.kwargs.items(), opts)

Eventually I found .exclude() which should do what I want for some
cases, but I think for more complex queriers (i.e. where you don't
want to negate the entire expression), I think it is a little cleaner
to write -Q () instead of QNot(), especially if QNot is buried. Then
again, it may be buried deliberately.

`-`would not be my first choice for something that does logical
negation, but it seems like it's either that or `~` (__invert__,
bit-wise negation operator).

-- 
This message has been scanned for memes and
dangerous content by MindScanner, and is
believed to be unclean.

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to