Ticket #3283: empty_queryset.patch

File empty_queryset.patch, 1.8 KB (added by medhat, 18 years ago)
  • db/models/manager.py

     
    1 from django.db.models.query import QuerySet
     1from django.db.models.query import QuerySet, EmptyQuerySet
    22from django.dispatch import dispatcher
    33from django.db.models import signals
    44from django.db.models.fields import FieldDoesNotExist
     
    4141    #######################
    4242    # PROXIES TO QUERYSET #
    4343    #######################
     44   
     45    def get_empty_query_set(self):
     46        return EmptyQuerySet(self.model)
    4447
    4548    def get_query_set(self):
    4649        """Returns a new QuerySet object.  Subclasses can override this method
    4750        to easily customise the behaviour of the Manager.
    4851        """
    4952        return QuerySet(self.model)
     53   
     54    def none(self):
     55        return self.get_empty_query_set()
    5056
    5157    def all(self):
    5258        return self.get_query_set()
  • db/models/query.py

     
    562562        c._kind = self._kind
    563563        c._order = self._order
    564564        return c
     565   
     566class EmptyQuerySet(QuerySet):
     567    def __init__(self, model=None):
     568        super(EmptyQuerySet,self).__init__(model)
     569        self._result_cache = []
     570       
     571    def iterator(self):
     572        raise StopIteration
     573       
     574    def count(self):
     575        return 0
     576       
     577    def delete(self):
     578        pass
    565579
     580    def _clone(self, klass=None, **kwargs):
     581        c = super(EmptyQuerySet, self)._clone(klass, **kwargs)
     582        c._result_cache = []
     583        return c
     584
    566585class QOperator(object):
    567586    "Base class for QAnd and QOr"
    568587    def __init__(self, *args):
Back to Top