From dd2ca8b0ea46a56aced70c46dc7283cf0cab5bef Mon Sep 17 00:00:00 2001 From: Carlton Gibson Date: Thu, 31 Oct 2019 11:34:56 +0100 Subject: [PATCH] [3.0.x] Fixed #30931 -- Restored ability to override Model.get_FIELD_display(). Thanks Sergey Fedoseev for the implementation idea. Regression in a68ea231012434b522ce45c513d84add516afa60. Backport of 2d38eb0ab9f78d68c083a5b78b1eca39027b279a from master --- django/db/models/fields/__init__.py | 8 ++++++-- docs/releases/2.2.7.txt | 3 +++ tests/model_fields/tests.py | 10 ++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index d073324745..37b0045ef3 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -767,8 +767,12 @@ class Field(RegisterLookupMixin): if not getattr(cls, self.attname, None): setattr(cls, self.attname, self.descriptor_class(self)) if self.choices is not None: - setattr(cls, 'get_%s_display' % self.name, - partialmethod(cls._get_FIELD_display, field=self)) + if not hasattr(cls, 'get_%s_display' % self.name): + setattr( + cls, + 'get_%s_display' % self.name, + partialmethod(cls._get_FIELD_display, field=self), + ) def get_filter_kwargs_for_object(self, obj): """ diff --git a/docs/releases/2.2.7.txt b/docs/releases/2.2.7.txt index cf1f52a685..537508225b 100644 --- a/docs/releases/2.2.7.txt +++ b/docs/releases/2.2.7.txt @@ -21,3 +21,6 @@ Bugfixes * Fixed migrations crash on PostgreSQL when adding an :class:`~django.db.models.Index` with fields ordering and :attr:`~.Index.opclasses` (:ticket:`30903`). + +* Restored the ability to override + :meth:`~django.db.models.Model.get_FOO_display` (:ticket:`30931`). diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py index abc5273d90..e399f68134 100644 --- a/tests/model_fields/tests.py +++ b/tests/model_fields/tests.py @@ -168,6 +168,16 @@ class GetFieldDisplayTests(SimpleTestCase): self.assertIsInstance(val, str) self.assertEqual(val, 'translated') + def test_overriding_FIELD_display(self): + class FooBar(models.Model): + foo_bar = models.IntegerField(choices=[(1, 'foo'), (2, 'bar')]) + + def get_foo_bar_display(self): + return 'something' + + f = FooBar(foo_bar=1) + self.assertEqual(f.get_foo_bar_display(), 'something') + def test_iterator_choices(self): """ get_choices() works with Iterators.