Fixed #30280 -- Restored Model.get_FIELD_display()'s coercion of lazy strings.
Reverted cc79c7ee63
.
This commit is contained in:
parent
d26b242443
commit
ea071870f9
|
@ -28,6 +28,7 @@ from django.db.models.signals import (
|
||||||
class_prepared, post_init, post_save, pre_init, pre_save,
|
class_prepared, post_init, post_save, pre_init, pre_save,
|
||||||
)
|
)
|
||||||
from django.db.models.utils import make_model_tuple
|
from django.db.models.utils import make_model_tuple
|
||||||
|
from django.utils.encoding import force_str
|
||||||
from django.utils.text import capfirst, get_text_list
|
from django.utils.text import capfirst, get_text_list
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from django.utils.version import get_version
|
from django.utils.version import get_version
|
||||||
|
@ -921,7 +922,8 @@ class Model(metaclass=ModelBase):
|
||||||
|
|
||||||
def _get_FIELD_display(self, field):
|
def _get_FIELD_display(self, field):
|
||||||
value = getattr(self, field.attname)
|
value = getattr(self, field.attname)
|
||||||
return dict(field.flatchoices).get(value, value)
|
# force_str() to coerce lazy strings.
|
||||||
|
return force_str(dict(field.flatchoices).get(value, value), strings_only=True)
|
||||||
|
|
||||||
def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs):
|
def _get_next_or_previous_by_FIELD(self, field, is_next, **kwargs):
|
||||||
if not self.pk:
|
if not self.pk:
|
||||||
|
|
|
@ -10,12 +10,13 @@ field. This method returns the "human-readable" value of the field.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
|
|
||||||
class Person(models.Model):
|
class Person(models.Model):
|
||||||
GENDER_CHOICES = (
|
GENDER_CHOICES = (
|
||||||
('M', 'Male'),
|
('M', _('Male')),
|
||||||
('F', 'Female'),
|
('F', _('Female')),
|
||||||
)
|
)
|
||||||
name = models.CharField(max_length=20)
|
name = models.CharField(max_length=20)
|
||||||
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
|
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)
|
||||||
|
|
|
@ -20,3 +20,6 @@ class ChoicesTests(TestCase):
|
||||||
|
|
||||||
a.gender = 'U'
|
a.gender = 'U'
|
||||||
self.assertEqual(a.get_gender_display(), 'U')
|
self.assertEqual(a.get_gender_display(), 'U')
|
||||||
|
|
||||||
|
# _get_FIELD_display() coerces lazy strings.
|
||||||
|
self.assertIsInstance(a.get_gender_display(), str)
|
||||||
|
|
Loading…
Reference in New Issue