Skip to content

Commit

Permalink
[3.0.x] Fixed #31155 -- Fixed a system check for the longest choice w…
Browse files Browse the repository at this point in the history
…hen a named group contains only non-string values.

Regression in b625195.

Thanks Murat Guchetl for the report.
Backport of 6f7998a from master
  • Loading branch information
felixxm committed Jan 11, 2020
1 parent 16297e7 commit 9286668
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions django/db/models/fields/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,10 +272,10 @@ def is_value(value, accept_promise=True):
):
break
if self.max_length is not None and group_choices:
choice_max_length = max(
choice_max_length = max([
choice_max_length,
*(len(value) for value, _ in group_choices if isinstance(value, str)),
)
])
except (TypeError, ValueError):
# No groups, choices in the form [value, display]
value, human_name = group_name, group_choices
Expand Down
3 changes: 3 additions & 0 deletions docs/releases/3.0.3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ Bugfixes

* Added support for using enumeration types ``TextChoices``,
``IntegerChoices``, and ``Choices`` in templates (:ticket:`31154`).

* Fixed a system check to ensure the ``max_length`` attribute fits the longest
choice, when a named group contains only non-string values (:ticket:`31155`).
18 changes: 18 additions & 0 deletions tests/invalid_models_tests/test_ordinary_fields.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import unittest
import uuid

from django.core.checks import Error, Warning as DjangoWarning
from django.db import connection, models
Expand Down Expand Up @@ -768,3 +769,20 @@ class Model(models.Model):
id='fields.W162',
)
])


@isolate_apps('invalid_models_tests')
class UUIDFieldTests(TestCase):
def test_choices_named_group(self):
class Model(models.Model):
field = models.UUIDField(
choices=[
['knights', [
[uuid.UUID('5c859437-d061-4847-b3f7-e6b78852f8c8'), 'Lancelot'],
[uuid.UUID('c7853ec1-2ea3-4359-b02d-b54e8f1bcee2'), 'Galahad'],
]],
[uuid.UUID('25d405be-4895-4d50-9b2e-d6695359ce47'), 'Other'],
],
)

self.assertEqual(Model._meta.get_field('field').check(), [])

0 comments on commit 9286668

Please sign in to comment.