Skip to content

Commit

Permalink
Fixed #35232 -- Cached model's Options.verbose_name_raw.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamchainz authored and felixxm committed Feb 19, 2024
1 parent 28a3fbe commit f25d84f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion django/db/models/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,11 @@ def can_migrate(self, connection):
)
return True

@property
@cached_property
def verbose_name_raw(self):
"""Return the untranslated verbose name."""
if isinstance(self.verbose_name, str):
return self.verbose_name
with override(None):
return str(self.verbose_name)

Expand Down
4 changes: 4 additions & 0 deletions tests/model_meta/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.utils.translation import gettext_lazy as _


class Relation(models.Model):
Expand Down Expand Up @@ -124,6 +125,9 @@ class Person(BasePerson):
# GR fields
generic_relation_concrete = GenericRelation(Relation)

class Meta:
verbose_name = _("Person")


class ProxyPerson(Person):
class Meta:
Expand Down
11 changes: 11 additions & 0 deletions tests/model_meta/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,17 @@ def test_get_fields_only_searches_forward_on_apps_not_ready(self):
opts.apps.models_ready = True


class VerboseNameRawTests(SimpleTestCase):
def test_string(self):
# Clear cached property.
Relation._meta.__dict__.pop("verbose_name_raw", None)
self.assertEqual(Relation._meta.verbose_name_raw, "relation")

def test_gettext(self):
Person._meta.__dict__.pop("verbose_name_raw", None)
self.assertEqual(Person._meta.verbose_name_raw, "Person")


class RelationTreeTests(SimpleTestCase):
all_models = (Relation, AbstractPerson, BasePerson, Person, ProxyPerson, Relating)

Expand Down

0 comments on commit f25d84f

Please sign in to comment.