Django

Code

Changeset 6518

Show
Ignore:
Timestamp:
10/14/07 22:32:11 (9 months ago)
Author:
mtredinnick
Message:

queryset-refactor: Added ~ support to Q-objects. Based heavily on a patch from
Collin Grady. Refs #4858.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/queryset-refactor/django/db/models/query_utils.py

    r6116 r6518  
    4141        return self._combine(other, self.AND) 
    4242 
     43    def __invert__(self): 
     44        return QNot(self) 
     45 
    4346class QNot(Q): 
    4447    """ 
     
    5154        self.negate() 
    5255 
     56    def __invert__(self): 
     57        return self.children[0] 
     58 
  • django/branches/queryset-refactor/docs/db-api.txt

    r6516 r6518  
    212212 
    213213Updating ``ForeignKey`` fields works exactly the same way as saving a normal 
    214 field; simply assign an object of the right type to the field in question::  
    215  
    216     cheese_blog = Blog.objects.get(name="Cheddar Talk")  
    217     entry.blog = cheese_blog  
    218     entry.save()  
     214field; simply assign an object of the right type to the field in question:: 
     215 
     216    cheese_blog = Blog.objects.get(name="Cheddar Talk") 
     217    entry.blog = cheese_blog 
     218    entry.save() 
    219219 
    220220Updating a ``ManyToManyField`` works a little differently; use the ``add()`` 
     
    15641564with the ``&`` and ``|`` operators. You can also use parenthetical grouping. 
    15651565 
     1566``Q`` objects can also be negated using the ``~`` operator, allowing for 
     1567combined lookups that combine both a normal query and a negated (``NOT``) 
     1568query:: 
     1569 
     1570    Q(question__startswith='Who') | ~Q(pub_date__year=2005) 
     1571 
    15661572Each lookup function that takes keyword-arguments (e.g. ``filter()``, 
    15671573``exclude()``, ``get()``) can also be passed one or more ``Q`` objects as 
  • django/branches/queryset-refactor/tests/modeltests/or_lookups/models.py

    r6489 r6518  
    9191[<Article: Hello and goodbye>] 
    9292 
     93# Q objects can be negated 
     94>>> Article.objects.filter(Q(pk=1) | ~Q(pk=2)) 
     95[<Article: Hello>, <Article: Hello and goodbye>] 
     96>>> Article.objects.filter(~Q(pk=1) & ~Q(pk=2)) 
     97[<Article: Hello and goodbye>] 
     98 
     99# This allows for more complex queries than filter() and exclude() alone would 
     100# allow 
     101>>> Article.objects.filter(Q(pk=1) & (~Q(pk=2) | Q(pk=3))) 
     102[<Article: Hello>] 
     103 
    93104# Try some arg queries with operations other than get_list 
    94105>>> Article.objects.get(Q(headline__startswith='Hello'), Q(headline__contains='bye'))