Ticket #17186: 17186-3.patch

File 17186-3.patch, 3.0 KB (added by Nate Bragg, 13 years ago)
  • django/db/models/expressions.py

    From 73dc570e13f9e96f4fa9752fd52f611fa3f334dd Mon Sep 17 00:00:00 2001
    From: Nate Bragg <jonathan.bragg@alum.rpi.edu>
    Date: Wed, 18 Jan 2012 22:02:12 -0500
    Subject: [PATCH] Implemented a fix to #17186 using a negation node
    
    ---
     django/db/models/expressions.py        |   14 ++++++++++++++
     tests/modeltests/expressions/models.py |    3 +++
     tests/modeltests/expressions/tests.py  |   10 ++++++++++
     3 files changed, 27 insertions(+), 0 deletions(-)
    
    diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py
    index a71f4a3..7c978dc 100644
    a b class ExpressionNode(tree.Node):  
    7070    def __or__(self, other):
    7171        return self._combine(other, self.OR, False)
    7272
     73    def __invert__(self):
     74        return NotNode([self])
     75
    7376    def __radd__(self, other):
    7477        return self._combine(other, self.ADD, True)
    7578
    class F(ExpressionNode):  
    113116    def evaluate(self, evaluator, qn, connection):
    114117        return evaluator.evaluate_leaf(self, qn, connection)
    115118
     119class NotNode(ExpressionNode):
     120    """
     121    Node that represents a negation.
     122    """
     123    def __init__(self, children=None):
     124        super(NotNode, self).__init__(children, None, True)
     125
     126    def evaluate(self, evaluator, qn, connection):
     127        sql, params = evaluator.evaluate_node(self, qn, connection)
     128        return ("NOT %s" % sql, params)
     129
    116130class DateModifierNode(ExpressionNode):
    117131    """
    118132    Node that implements the following syntax:
  • tests/modeltests/expressions/models.py

    diff --git a/tests/modeltests/expressions/models.py b/tests/modeltests/expressions/models.py
    index dd50499..da6f1df 100644
    a b from django.db import models  
    88class Employee(models.Model):
    99    firstname = models.CharField(max_length=50)
    1010    lastname = models.CharField(max_length=50)
     11    is_active = models.BooleanField(default=False)
    1112
    1213    def __unicode__(self):
    1314        return u'%s %s' % (self.firstname, self.lastname)
    class Company(models.Model):  
    2627
    2728    def __unicode__(self):
    2829        return self.name
     30
     31
  • tests/modeltests/expressions/tests.py

    diff --git a/tests/modeltests/expressions/tests.py b/tests/modeltests/expressions/tests.py
    index 8f4f546..59ae91b 100644
    a b class ExpressionsTests(TestCase):  
    2222            ceo=Employee.objects.create(firstname="Max", lastname="Mustermann")
    2323        )
    2424
     25        # mark acttive employees when their companys have num_chairs great that 3
     26        employee_qs = Employee.objects.filter(is_active=False)
     27        self.assertEqual(employee_qs.count(), 3)
     28
     29        Employee.objects.filter(company_ceo_set__num_chairs__gte=3)\
     30            .update(is_active=~F("is_active"))
     31
     32        employee_qs = Employee.objects.filter(is_active=False)
     33        self.assertEqual(employee_qs.count(), 1)
     34
    2535        company_query = Company.objects.values(
    2636            "name", "num_employees", "num_chairs"
    2737        ).order_by(
Back to Top