Author: mtredinnick
Date: 2007-10-14 22:32:11 -0500 (Sun, 14 Oct 2007)
New Revision: 6518
Modified:
django/branches/queryset-refactor/django/db/models/query_utils.py
django/branches/queryset-refactor/docs/db-api.txt
django/branches/queryset-refactor/tests/modeltests/or_lookups/models.py
Log:
queryset-refactor: Added ~ support to Q-objects. Based heavily on a patch from
Collin Grady. Refs #4858.
Modified: django/branches/queryset-refactor/django/db/models/query_utils.py
===================================================================
--- django/branches/queryset-refactor/django/db/models/query_utils.py
2007-10-15 03:31:37 UTC (rev 6517)
+++ django/branches/queryset-refactor/django/db/models/query_utils.py
2007-10-15 03:32:11 UTC (rev 6518)
@@ -40,6 +40,9 @@
def __and__(self, other):
return self._combine(other, self.AND)
+ def __invert__(self):
+ return QNot(self)
+
class QNot(Q):
"""
Encapsulates the negation of a Q object.
@@ -50,3 +53,6 @@
self.add(q, self.AND)
self.negate()
+ def __invert__(self):
+ return self.children[0]
+
Modified: django/branches/queryset-refactor/docs/db-api.txt
===================================================================
--- django/branches/queryset-refactor/docs/db-api.txt 2007-10-15 03:31:37 UTC
(rev 6517)
+++ django/branches/queryset-refactor/docs/db-api.txt 2007-10-15 03:32:11 UTC
(rev 6518)
@@ -211,11 +211,11 @@
--------------------------------------------
Updating ``ForeignKey`` fields works exactly the same way as saving a normal
-field; simply assign an object of the right type to the field in question::
+field; simply assign an object of the right type to the field in question::
- cheese_blog = Blog.objects.get(name="Cheddar Talk")
- entry.blog = cheese_blog
- entry.save()
+ cheese_blog = Blog.objects.get(name="Cheddar Talk")
+ entry.blog = cheese_blog
+ entry.save()
Updating a ``ManyToManyField`` works a little differently; use the ``add()``
method on the field to add a record to the relation::
@@ -1563,6 +1563,12 @@
You can compose statements of arbitrary complexity by combining ``Q`` objects
with the ``&`` and ``|`` operators. You can also use parenthetical grouping.
+``Q`` objects can also be negated using the ``~`` operator, allowing for
+combined lookups that combine both a normal query and a negated (``NOT``)
+query::
+
+ Q(question__startswith='Who') | ~Q(pub_date__year=2005)
+
Each lookup function that takes keyword-arguments (e.g. ``filter()``,
``exclude()``, ``get()``) can also be passed one or more ``Q`` objects as
positional (not-named) arguments. If you provide multiple ``Q`` object
Modified:
django/branches/queryset-refactor/tests/modeltests/or_lookups/models.py
===================================================================
--- django/branches/queryset-refactor/tests/modeltests/or_lookups/models.py
2007-10-15 03:31:37 UTC (rev 6517)
+++ django/branches/queryset-refactor/tests/modeltests/or_lookups/models.py
2007-10-15 03:32:11 UTC (rev 6518)
@@ -90,6 +90,17 @@
>>> Article.objects.filter(Q(headline__contains='bye'),
>>> headline__startswith='Hello')
[<Article: Hello and goodbye>]
+# Q objects can be negated
+>>> Article.objects.filter(Q(pk=1) | ~Q(pk=2))
+[<Article: Hello>, <Article: Hello and goodbye>]
+>>> Article.objects.filter(~Q(pk=1) & ~Q(pk=2))
+[<Article: Hello and goodbye>]
+
+# This allows for more complex queries than filter() and exclude() alone would
+# allow
+>>> Article.objects.filter(Q(pk=1) & (~Q(pk=2) | Q(pk=3)))
+[<Article: Hello>]
+
# Try some arg queries with operations other than get_list
>>> Article.objects.get(Q(headline__startswith='Hello'),
>>> Q(headline__contains='bye'))
<Article: Hello and goodbye>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" 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-updates?hl=en
-~----------~----~----~----~------~----~------~--~---