diff options
| author | CruxBox <shubhambhagat111@yahoo.com> | 2019-06-10 03:32:40 +0530 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-06-10 19:48:53 +0200 |
| commit | 34f8eeea4aaed37e1d02915b76c18892c8c1718e (patch) | |
| tree | 7d07bce49be3aee9f1e3e8ae5e8bfeea88dd4add | |
| parent | 03cd3d137e2c29484b020b9768a4741f1096be97 (diff) | |
Fixed #30548 -- Improved exception when expression contains mixed types.
| -rw-r--r-- | django/db/models/expressions.py | 11 | ||||
| -rw-r--r-- | tests/aggregation/tests.py | 5 |
2 files changed, 13 insertions, 3 deletions
diff --git a/django/db/models/expressions.py b/django/db/models/expressions.py index 16df317631..36f88f99ec 100644 --- a/django/db/models/expressions.py +++ b/django/db/models/expressions.py @@ -286,8 +286,15 @@ class BaseExpression: """ sources_iter = (source for source in self.get_source_fields() if source is not None) for output_field in sources_iter: - if any(not isinstance(output_field, source.__class__) for source in sources_iter): - raise FieldError('Expression contains mixed types. You must set output_field.') + for source in sources_iter: + if not isinstance(output_field, source.__class__): + raise FieldError( + 'Expression contains mixed types: %s, %s. You must ' + 'set output_field.' % ( + output_field.__class__.__name__, + source.__class__.__name__, + ) + ) return output_field @staticmethod diff --git a/tests/aggregation/tests.py b/tests/aggregation/tests.py index a336758f45..bd6ecf699c 100644 --- a/tests/aggregation/tests.py +++ b/tests/aggregation/tests.py @@ -884,7 +884,10 @@ class AggregateTestCase(TestCase): self.assertEqual(p2, {'avg_price': Approximate(Decimal('53.39'), places=2)}) def test_combine_different_types(self): - msg = 'Expression contains mixed types. You must set output_field.' + msg = ( + 'Expression contains mixed types: FloatField, IntegerField. ' + 'You must set output_field.' + ) qs = Book.objects.annotate(sums=Sum('rating') + Sum('pages') + Sum('price')) with self.assertRaisesMessage(FieldError, msg): qs.first() |
