Django

Code

Changeset 2422

Show
Ignore:
Timestamp:
02/27/06 16:51:36 (3 years ago)
Author:
jacob
Message:

Added QuerySet?.exclude() which does the opposite of QuerySet?.filter(). As a side effect, the "ne" lookup type no longer exists. This fixes #966.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/magic-removal/django/db/backends/ado_mssql/base.py

    r1999 r2422  
    120120    'contains': 'LIKE %s', 
    121121    'icontains': 'LIKE %s', 
    122     'ne': '!= %s', 
    123122    'gt': '> %s', 
    124123    'gte': '>= %s', 
  • django/branches/magic-removal/django/db/backends/mysql/base.py

    r2033 r2422  
    133133    'contains': 'LIKE BINARY %s', 
    134134    'icontains': 'LIKE %s', 
    135     'ne': '!= %s', 
    136135    'gt': '> %s', 
    137136    'gte': '>= %s', 
  • django/branches/magic-removal/django/db/backends/postgresql/base.py

    r1999 r2422  
    108108    'contains': 'LIKE %s', 
    109109    'icontains': 'ILIKE %s', 
    110     'ne': '!= %s', 
    111110    'gt': '> %s', 
    112111    'gte': '>= %s', 
  • django/branches/magic-removal/django/db/backends/sqlite3/base.py

    r1999 r2422  
    132132    'contains': "LIKE %s ESCAPE '\\'", 
    133133    'icontains': "LIKE %s ESCAPE '\\'", 
    134     'ne': '!= %s', 
    135134    'gt': '> %s', 
    136135    'gte': '>= %s', 
  • django/branches/magic-removal/django/db/models/manager.py

    r2409 r2422  
    7070        return self.get_query_set().filter(*args, **kwargs) 
    7171 
     72    def exclude(self, *args, **kwargs): 
     73        return self.get_query_set().exclude(*args, **kwargs) 
     74 
    7275    def in_bulk(self, *args, **kwargs): 
    7376        return self.get_query_set().in_bulk(*args, **kwargs) 
  • django/branches/magic-removal/django/db/models/query.py

    r2393 r2422  
    245245    def filter(self, *args, **kwargs): 
    246246        "Returns a new QuerySet instance with the args ANDed to the existing set." 
     247        return self._filter_or_exclude(Q, *args, **kwargs)     
     248         
     249    def exclude(self, *args, **kwargs): 
     250        "Returns a new QuerySet instance with NOT (arsg) ANDed to the existing set." 
     251        return self._filter_or_exclude(QNot, *args, **kwargs) 
     252         
     253    def _filter_or_exclude(self, qtype, *args, **kwargs): 
    247254        clone = self._clone() 
    248255        if len(kwargs) > 0: 
    249             clone._filters = clone._filters & Q(**kwargs) 
     256            clone._filters = clone._filters & qtype(**kwargs) 
    250257        if len(args) > 0: 
    251258            clone._filters = clone._filters & reduce(operator.and_, args) 
     
    491498            raise TypeError, other 
    492499 
    493 class Q
     500class Q(object)
    494501    "Encapsulates queries as objects that can be combined logically." 
    495502    def __init__(self, **kwargs): 
     
    504511    def get_sql(self, opts): 
    505512        return parse_lookup(self.kwargs.items(), opts) 
     513 
     514class QNot(Q): 
     515    "Encapsulates NOT (...) queries as objects" 
     516     
     517    def get_sql(self, opts): 
     518        tables, joins, where, params = super(QNot, self).get_sql(opts) 
     519        where2 = ['(NOT (%s))' % " AND ".join(where)] 
     520        return tables, joins, where2, params 
    506521 
    507522def get_where_clause(lookup_type, table_prefix, field_name, value): 
  • django/branches/magic-removal/tests/modeltests/lookup/models.py

    r2200 r2422  
    171171>>> Article.objects.filter(headline__startswith='Article%') 
    172172[Article% with percent sign] 
     173 
     174# exclude() is the opposite of filter() when doing lookups: 
     175>>> Article.objects.filter(headline__contains='Article').exclude(headline__contains='with') 
     176[Article 5, Article 6, Article 4, Article 2, Article 3, Article 7, Article 1] 
     177>>> Article.objects.exclude(headline__startswith="Article_") 
     178[Article% with percent sign, Article 5, Article 6, Article 4, Article 2, Article 3, Article 7, Article 1] 
     179>>> Article.objects.exclude(headline="Article 7") 
     180[Article% with percent sign, Article_ with underscore, Article 5, Article 6, Article 4, Article 2, Article 3, Article 1]cl 
    173181"""