[Django] #35566: Support get_FOO_display() on GeneratedField

33 views
Skip to first unread message

Django

unread,
Jun 28, 2024, 8:11:29 AM6/28/24
to django-...@googlegroups.com
#35566: Support get_FOO_display() on GeneratedField
-------------------------------------+-------------------------------------
Reporter: | Owner: nobody
roniemartinez |
Type: New | Status: new
feature |
Component: Database | Version: 5.0
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Please add support for get_FOO_display() on GeneratedField.

We initially used a normal IntegerField with choices
[IntegerField(choices=choices, ...)] and then running a SQL to make this
field a generated field. We then migrated to GeneratedField while
maintaining the output_field similar to our old one.

However, we found that get_FOO_display() don't work anymore and we had to
write a method for this, instead.
--
Ticket URL: <https://code.djangoproject.com/ticket/35566>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 1, 2024, 5:01:52 AM7/1/24
to django-...@googlegroups.com
#35566: Support get_FOO_display() on GeneratedField
-------------------------------------+-------------------------------------
Reporter: Ronie Martinez | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Sarah Boyce):

Hello Ronie!

I think that the use of "choices" in a GeneratedField is key here as
there's some specific logic for get_FIELD_display() when choices is set.

For me to be sure, can you share what error you received here (or some
more specifics on how it didn't work if there's no error)?

If you got an error like: `AttributeError: 'Bar' object has no attribute
'get_FOO_display'. Did you mean: 'get_something_else_display'?`

Then I think I understand the issue and have replicated (sample failing
test attached below).

It's also possible you received this error: `AttributeError: Cannot read a
generated field from an unsaved model.` if `get_FOO_display` was called
before the model saved (related to #35560). If this is the issue, can you
share a bit more about when this gets called? Maybe sharing your model's
GeneratedField would also help 🙂


{{{#!diff
diff --git a/tests/model_fields/models.py b/tests/model_fields/models.py
index 652c808b40..3cc856db83 100644
--- a/tests/model_fields/models.py
+++ b/tests/model_fields/models.py
@@ -72,6 +72,18 @@ class WhizIterEmpty(models.Model):
c = models.CharField(choices=iter(()), blank=True, max_length=1)


+class WhizGenerated(models.Model):
+ c = models.IntegerField(choices=Whiz.CHOICES, null=True)
+ c_copy = models.GeneratedField(
+ expression=F("c"),
+ output_field=models.IntegerField(choices=Whiz.CHOICES,
null=True),
+ db_persist=True,
+ )
+
+ class Meta:
+ required_db_features = {"supports_stored_generated_columns"}
+
+
class Choiceful(models.Model):
class Suit(models.IntegerChoices):
DIAMOND = 1, "Diamond"
diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py
index 36e54d4b8b..8d7caf7072 100644
--- a/tests/model_fields/tests.py
+++ b/tests/model_fields/tests.py
@@ -17,6 +17,7 @@ from .models import (
WhizDelayed,
WhizIter,
WhizIterEmpty,
+ WhizGenerated,
)


@@ -297,6 +298,16 @@ class GetFieldDisplayTests(SimpleTestCase):
self.assertEqual(WhizIterEmpty(c="").c, "") # Empty value


+class GetFieldDisplayGeneratedTests(TestCase):
+ def test_choices_and_field_display(self):
+ wg_0 = WhizGenerated.objects.create(c=0)
+ wg_1 = WhizGenerated.objects.create(c=1)
+ wg_none = WhizGenerated.objects.create(c=None)
+ self.assertEqual(wg_0.get_c_copy_display(), "Other")
+ self.assertEqual(wg_1.get_c_copy_display(), "First")
+ self.assertIsNone(wg_none.get_c_display())
+
+
class GetChoicesTests(SimpleTestCase):
def test_empty_choices(self):
choices = []
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/35566#comment:1>

Django

unread,
Jul 1, 2024, 5:37:29 AM7/1/24
to django-...@googlegroups.com
#35566: Support get_FOO_display() on GeneratedField
-------------------------------------+-------------------------------------
Reporter: Ronie Martinez | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* stage: Unreviewed => Accepted

Comment:

Read through the docs for
[https://docs.djangoproject.com/en/5.0/ref/models/instances/#django.db.models.Model.get_FOO_display
Model.get_FOO_display()]
As this technically doesn't imply this will be supported for a
`GeneratedField`, agree this can be considered a new feature and not a
release blocker 👍
--
Ticket URL: <https://code.djangoproject.com/ticket/35566#comment:2>

Django

unread,
Jul 1, 2024, 6:43:29 AM7/1/24
to django-...@googlegroups.com
#35566: Support get_FOO_display() on GeneratedField
-------------------------------------+-------------------------------------
Reporter: Ronie Martinez | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Ronie Martinez):

Hi Sarah,

We are getting `'MyModel' object has no attribute 'get_FOO_display'`.

Thanks for accepting this feature.
--
Ticket URL: <https://code.djangoproject.com/ticket/35566#comment:3>

Django

unread,
Jul 1, 2024, 9:14:13 AM7/1/24
to django-...@googlegroups.com
#35566: Support get_FOO_display() on GeneratedField
-------------------------------------+-------------------------------------
Reporter: Ronie Martinez | Owner: Sarah
| Boyce
Type: New feature | Status: assigned
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* has_patch: 0 => 1
* owner: nobody => Sarah Boyce
* status: new => assigned

Comment:

Replying to [comment:3 Ronie Martinez]:
> Thanks for accepting this feature.

You're very welcome, thank you for raising 👍
--
Ticket URL: <https://code.djangoproject.com/ticket/35566#comment:4>

Django

unread,
Jul 1, 2024, 5:47:24 PM7/1/24
to django-...@googlegroups.com
#35566: Support get_FOO_display() on GeneratedField
-------------------------------------+-------------------------------------
Reporter: Ronie Martinez | Owner: Sarah
| Boyce
Type: New feature | Status: assigned
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35566#comment:5>

Django

unread,
Sep 1, 2024, 10:15:53 AM9/1/24
to django-...@googlegroups.com
#35566: Support get_FOO_display() on GeneratedField
-------------------------------------+-------------------------------------
Reporter: Ronie Martinez | Owner: Sarah
| Boyce
Type: New feature | Status: assigned
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* needs_better_patch: 1 => 0

--
Ticket URL: <https://code.djangoproject.com/ticket/35566#comment:6>

Django

unread,
Oct 12, 2024, 10:35:24 AM10/12/24
to django-...@googlegroups.com
#35566: Support get_FOO_display() on GeneratedField
-------------------------------------+-------------------------------------
Reporter: Ronie Martinez | Owner: Sarah
| Boyce
Type: New feature | Status: assigned
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Timothy Schilling):

* cc: Timothy Schilling (added)
* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/35566#comment:7>

Django

unread,
Oct 12, 2024, 10:40:00 AM10/12/24
to django-...@googlegroups.com
#35566: Support get_FOO_display() on GeneratedField
-------------------------------------+-------------------------------------
Reporter: Ronie Martinez | Owner: Sarah
| Boyce
Type: New feature | Status: assigned
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Raffaella):

* cc: Raffaella (added)

--
Ticket URL: <https://code.djangoproject.com/ticket/35566#comment:8>

Django

unread,
Oct 14, 2024, 4:47:33 AM10/14/24
to django-...@googlegroups.com
#35566: Support get_FOO_display() on GeneratedField
-------------------------------------+-------------------------------------
Reporter: Ronie Martinez | Owner: Sarah
| Boyce
Type: New feature | Status: closed
Component: Database layer | Version: 5.0
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* resolution: => wontfix
* status: assigned => closed

Comment:

Hi Ronie, I'm closing this ticket as GeneratedField's do support
`get_FOO_display()` but choices need to be set on the `GeneratedField`
rather than on the `output_field` (which is consistent to other fields)
--
Ticket URL: <https://code.djangoproject.com/ticket/35566#comment:9>
Reply all
Reply to author
Forward
0 new messages