diff options
| author | toan <toan.vuong@hyperscience.com> | 2023-09-21 15:51:45 -0700 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2023-09-22 09:33:50 +0200 |
| commit | 4de31ec680df062e5964b630f1b881ead5004e15 (patch) | |
| tree | fedffba2e27c1585a647b1bd97f30a7538cd7cf0 | |
| parent | d7972436639bacada0f94d3b9818446343af59ad (diff) | |
Fixed #34858 -- Corrected resolving output_field for PositiveIntegerField.
Regression in 40b8a6174f001a310aa33f7880db0efeeb04d4c4.
| -rw-r--r-- | AUTHORS | 1 | ||||
| -rw-r--r-- | django/db/models/expressions.py | 19 | ||||
| -rw-r--r-- | tests/expressions/tests.py | 18 |
3 files changed, 38 insertions, 0 deletions
@@ -978,6 +978,7 @@ answer newbie questions, and generally made Django that much better: Tim Heap <tim@timheap.me> Tim McCurrach <tim.mccurrach@gmail.com> Tim Saylor <tim.saylor@gmail.com> + Toan Vuong <me@toanvuong.io> Tobias Kunze <rixx@cutebit.de> Tobias McNulty <https://www.caktusgroup.com/blog/> tobias@neuyork.de diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py index 4ea179ecde..30d44650ec 100644 --- a/django/db/models/expressions.py +++ b/django/db/models/expressions.py @@ -512,6 +512,25 @@ class Expression(BaseExpression, Combinable): _connector_combinations = [ # Numeric operations - operands of same type. + # PositiveIntegerField should take precedence over IntegerField (except + # subtraction). + { + connector: [ + ( + fields.PositiveIntegerField, + fields.PositiveIntegerField, + fields.PositiveIntegerField, + ), + ] + for connector in ( + Combinable.ADD, + Combinable.MUL, + Combinable.DIV, + Combinable.MOD, + Combinable.POW, + ) + }, + # Other numeric operands. { connector: [ (fields.IntegerField, fields.IntegerField, fields.IntegerField), diff --git a/tests/expressions/tests.py b/tests/expressions/tests.py index 4bd46762e1..f26a6275ea 100644 --- a/tests/expressions/tests.py +++ b/tests/expressions/tests.py @@ -34,6 +34,7 @@ from django.db.models import ( Model, OrderBy, OuterRef, + PositiveIntegerField, Q, StdDev, Subquery, @@ -2455,6 +2456,23 @@ class CombinableTests(SimpleTestCase): class CombinedExpressionTests(SimpleTestCase): + def test_resolve_output_field_positive_integer(self): + connectors = [ + Combinable.ADD, + Combinable.MUL, + Combinable.DIV, + Combinable.MOD, + Combinable.POW, + ] + for connector in connectors: + with self.subTest(connector=connector): + expr = CombinedExpression( + Expression(PositiveIntegerField()), + connector, + Expression(PositiveIntegerField()), + ) + self.assertIsInstance(expr.output_field, PositiveIntegerField) + def test_resolve_output_field_number(self): tests = [ (IntegerField, AutoField, IntegerField), |
