diff --git a/django/contrib/auth/context_processors.py b/django/contrib/auth/context_processors.py index 3ffab01e94..1b6c2eedd0 100644 --- a/django/contrib/auth/context_processors.py +++ b/django/contrib/auth/context_processors.py @@ -11,8 +11,9 @@ class PermLookupDict(object): def __getitem__(self, perm_name): return self.user.has_perm("%s.%s" % (self.module_name, perm_name)) - def __nonzero__(self): + def __bool__(self): return self.user.has_module_perms(self.module_name) + __nonzero__ = __bool__ # Python 2 class PermWrapper(object): diff --git a/django/core/files/base.py b/django/core/files/base.py index 37b1be89b3..d0b25250a5 100644 --- a/django/core/files/base.py +++ b/django/core/files/base.py @@ -26,8 +26,9 @@ class File(FileProxyMixin): def __repr__(self): return "<%s: %s>" % (self.__class__.__name__, self or "None") - def __nonzero__(self): + def __bool__(self): return bool(self.name) + __nonzero__ = __bool__ # Python 2 def __len__(self): return self.size @@ -135,8 +136,9 @@ class ContentFile(File): def __str__(self): return 'Raw content' - def __nonzero__(self): + def __bool__(self): return True + __nonzero__ = __bool__ # Python 2 def open(self, mode=None): self.seek(0) diff --git a/django/db/models/query.py b/django/db/models/query.py index 6013233a0f..4441c1a4ef 100644 --- a/django/db/models/query.py +++ b/django/db/models/query.py @@ -120,7 +120,7 @@ class QuerySet(object): if len(self._result_cache) <= pos: self._fill_cache() - def __nonzero__(self): + def __bool__(self): if self._prefetch_related_lookups and not self._prefetch_done: # We need all the results in order to be able to do the prefetch # in one go. To minimize code duplication, we use the __len__ @@ -134,6 +134,7 @@ class QuerySet(object): except StopIteration: return False return True + __nonzero__ = __bool__ # Python 2 def __contains__(self, val): # The 'in' operator works without this method, due to __iter__. This diff --git a/django/dispatch/saferef.py b/django/dispatch/saferef.py index 2a2c8739fb..d8728e219a 100644 --- a/django/dispatch/saferef.py +++ b/django/dispatch/saferef.py @@ -152,9 +152,10 @@ class BoundMethodWeakref(object): __repr__ = __str__ - def __nonzero__( self ): + def __bool__( self ): """Whether we are still a valid reference""" return self() is not None + __nonzero__ = __bool__ # Python 2 def __eq__(self, other): """Compare with another reference""" diff --git a/django/forms/formsets.py b/django/forms/formsets.py index 1ec8340462..4ea8dc4ca9 100644 --- a/django/forms/formsets.py +++ b/django/forms/formsets.py @@ -65,9 +65,10 @@ class BaseFormSet(StrAndUnicode): def __len__(self): return len(self.forms) - def __nonzero__(self): + def __bool__(self): """All formsets have a management form which is not included in the length""" return True + __nonzero__ = __bool__ # Python 2 def _management_form(self): """Returns the ManagementForm instance for this FormSet.""" diff --git a/django/utils/tree.py b/django/utils/tree.py index 36b5977942..717181d2b9 100644 --- a/django/utils/tree.py +++ b/django/utils/tree.py @@ -68,11 +68,12 @@ class Node(object): """ return len(self.children) - def __nonzero__(self): + def __bool__(self): """ For truth value testing. """ return bool(self.children) + __nonzero__ = __bool__ # Python 2 def __contains__(self, other): """ diff --git a/docs/topics/db/optimization.txt b/docs/topics/db/optimization.txt index 573e1fd0aa..772792d39d 100644 --- a/docs/topics/db/optimization.txt +++ b/docs/topics/db/optimization.txt @@ -230,7 +230,7 @@ It is optimal because: #. Use of :ttag:`with` means that we store ``user.emails.all`` in a variable for later use, allowing its cache to be re-used. -#. The line ``{% if emails %}`` causes ``QuerySet.__nonzero__()`` to be called, +#. The line ``{% if emails %}`` causes ``QuerySet.__bool__()`` to be called, which causes the ``user.emails.all()`` query to be run on the database, and at the least the first line to be turned into an ORM object. If there aren't any results, it will return False, otherwise True.