Kaydet (Commit) 617d5f41 authored tarafından priyanshsaxena's avatar priyanshsaxena Kaydeden (comit) Tim Graham

Fixed #29066 -- Allowed negating query expressions.

üst 6b2f8fb9
...@@ -662,6 +662,7 @@ answer newbie questions, and generally made Django that much better: ...@@ -662,6 +662,7 @@ answer newbie questions, and generally made Django that much better:
pradeep.gowda@gmail.com pradeep.gowda@gmail.com
Preston Holmes <preston@ptone.com> Preston Holmes <preston@ptone.com>
Preston Timmons <prestontimmons@gmail.com> Preston Timmons <prestontimmons@gmail.com>
Priyansh Saxena <askpriyansh@gmail.com>
Rachel Tobin <rmtobin@me.com> Rachel Tobin <rmtobin@me.com>
Rachel Willmer <http://www.willmer.com/kb/> Rachel Willmer <http://www.willmer.com/kb/>
Radek Švarz <http://www.svarz.cz/translate/> Radek Švarz <http://www.svarz.cz/translate/>
......
...@@ -65,6 +65,9 @@ class Combinable: ...@@ -65,6 +65,9 @@ class Combinable:
# OPERATORS # # OPERATORS #
############# #############
def __neg__(self):
return self._combine(-1, self.MUL, False)
def __add__(self, other): def __add__(self, other):
return self._combine(other, self.ADD, False) return self._combine(other, self.ADD, False)
......
...@@ -13,9 +13,13 @@ more complex computations. ...@@ -13,9 +13,13 @@ more complex computations.
Supported arithmetic Supported arithmetic
==================== ====================
Django supports addition, subtraction, multiplication, division, modulo Django supports negation, addition, subtraction, multiplication, division,
arithmetic, and the power operator on query expressions, using Python constants, modulo arithmetic, and the power operator on query expressions, using Python
variables, and even other expressions. constants, variables, and even other expressions.
.. versionchanged:: 2.1
Support for negation was added.
Some examples Some examples
============= =============
......
...@@ -178,6 +178,8 @@ Models ...@@ -178,6 +178,8 @@ Models
:class:`~django.db.models.DateField` and :class:`~django.db.models.DateField` and
:class:`~django.db.models.DateTimeField` to the Monday of a week. :class:`~django.db.models.DateTimeField` to the Monday of a week.
* Query expressions can now be negated using a minus sign.
Requests and Responses Requests and Responses
~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~
......
...@@ -1425,6 +1425,10 @@ class ReprTests(TestCase): ...@@ -1425,6 +1425,10 @@ class ReprTests(TestCase):
class CombinableTests(SimpleTestCase): class CombinableTests(SimpleTestCase):
bitwise_msg = 'Use .bitand() and .bitor() for bitwise logical operations.' bitwise_msg = 'Use .bitand() and .bitor() for bitwise logical operations.'
def test_negation(self):
c = Combinable()
self.assertEqual(-c, c * -1)
def test_and(self): def test_and(self):
with self.assertRaisesMessage(NotImplementedError, self.bitwise_msg): with self.assertRaisesMessage(NotImplementedError, self.bitwise_msg):
Combinable() & Combinable() Combinable() & Combinable()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment