Ticket #3541: cache_count.patch

File cache_count.patch, 1.0 KB (added by Chris Beaven, 18 years ago)
  • django/db/models/query.py

     
    194194                yield obj
    195195
    196196    def count(self):
    197         "Performs a SELECT COUNT() and returns the number of records as an integer."
     197        """
     198        Returns the number of records as an integer.
     199       
     200        If _result_cache has been loaded, do len(self._result_cache)
     201        otherwise perform a SELECT COUNT().
     202        """
     203        if hasattr(self, '_result_count'):
     204            return self._result_count
     205        if self._result_cache is not None:
     206            self._result_count = len(self._result_cache)
     207            return self._result_count
    198208        counter = self._clone()
    199209        counter._order_by = ()
    200210        counter._select_related = False
     
    225235        if limit:
    226236            count = min(limit, count)
    227237
     238        self._result_count = count
    228239        return count
    229240
    230241    def get(self, *args, **kwargs):
Back to Top