Ticket #17186: invert.f-2.patch
File invert.f-2.patch, 2.5 KB (added by , 13 years ago) |
---|
-
tests/modeltests/expressions/tests.py
22 22 ceo=Employee.objects.create(firstname="Max", lastname="Mustermann") 23 23 ) 24 24 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 25 35 company_query = Company.objects.values( 26 36 "name", "num_employees", "num_chairs" 27 37 ).order_by( -
tests/modeltests/expressions/models.py
8 8 class Employee(models.Model): 9 9 firstname = models.CharField(max_length=50) 10 10 lastname = models.CharField(max_length=50) 11 is_active = models.BooleanField(default=False) 11 12 12 13 def __unicode__(self): 13 14 return u'%s %s' % (self.firstname, self.lastname) … … 26 27 27 28 def __unicode__(self): 28 29 return self.name 30 31 -
django/db/models/expressions.py
+
98 98 """ 99 99 An expression representing the value of the given field. 100 100 """ 101 102 _invert = False 103 101 104 def __init__(self, name): 102 105 super(F, self).__init__(None, None, False) 103 106 self.name = name … … 107 110 obj.name = self.name 108 111 return obj 109 112 113 def __invert__(self): 114 self._invert = True 115 return self 116 110 117 def prepare(self, evaluator, query, allow_joins): 111 118 return evaluator.prepare_leaf(self, query, allow_joins) 112 119 113 120 def evaluate(self, evaluator, qn, connection): 114 return evaluator.evaluate_leaf(self, qn, connection) 121 evresult = list(evaluator.evaluate_leaf(self, qn, connection)) 122 if self._invert: 123 evresult[0] = "NOT %s" % evresult[0] 124 return tuple(evresult) 115 125 126 116 127 class DateModifierNode(ExpressionNode): 117 128 """ 118 129 Node that implements the following syntax: