diff --git a/CHANGES.rst b/CHANGES.rst index de41a41..65dac73 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -4,6 +4,8 @@ CHANGES tip (unreleased) ---------------- +- Fixed lack of ``get_FOO_display`` method for ``StatusField``. Fixes GH-41. + 1.3.1 (2013.04.11) ------------------ @@ -11,8 +13,10 @@ tip (unreleased) - Added explicit default to ``BooleanField`` in tests, for Django trunk compatibility. -- Fix intermittent ``StatusField`` bug. Fixes GH-29. +- Fixed intermittent ``StatusField`` bug. Fixes GH-29. + - Added Python 3 support + - Dropped support for Django 1.2 and 1.3. Django 1.4.2+ required. diff --git a/model_utils/fields.py b/model_utils/fields.py index 7d46504..5aebb3c 100644 --- a/model_utils/fields.py +++ b/model_utils/fields.py @@ -61,6 +61,10 @@ class StatusField(models.CharField): def contribute_to_class(self, cls, name): models.signals.class_prepared.connect(self.prepare_class, sender=cls) + # we don't set the real choices until class_prepared (so we can rely on + # the STATUS class attr being available), but we need to set some dummy + # choices now so the super method will add the get_FOO_display method + self._choices = [(0, 'dummy')] super(StatusField, self).contribute_to_class(cls, name) diff --git a/model_utils/tests/tests.py b/model_utils/tests/tests.py index d2ce0d1..3331ac3 100644 --- a/model_utils/tests/tests.py +++ b/model_utils/tests/tests.py @@ -181,6 +181,10 @@ class StatusFieldTests(TestCase): # this model has no STATUS attribute, so checking for it would error field.prepare_class(Article) + def test_get_status_display(self): + instance = StatusFieldDefaultFilled() + self.assertEqual(instance.get_status_display(), "Yes") + class ChoicesTests(TestCase): def setUp(self):