Opened 5 months ago

Closed 6 weeks ago

#35566 closed New feature (wontfix)

Support get_FOO_display() on GeneratedField

Reported by: Ronie Martinez Owned by: Sarah Boyce
Component: Database layer (models, ORM) Version: 5.0
Severity: Normal Keywords:
Cc: Ronie Martinez, Timothy Schilling, Raffaella Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

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.

Change History (9)

comment:1 by Sarah Boyce, 5 months ago

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 🙂

  • tests/model_fields/models.py

    diff --git a/tests/model_fields/models.py b/tests/model_fields/models.py
    index 652c808b40..3cc856db83 100644
    a b class WhizIterEmpty(models.Model):  
    7272    c = models.CharField(choices=iter(()), blank=True, max_length=1)
    7373
    7474
     75class WhizGenerated(models.Model):
     76    c = models.IntegerField(choices=Whiz.CHOICES, null=True)
     77    c_copy = models.GeneratedField(
     78        expression=F("c"),
     79        output_field=models.IntegerField(choices=Whiz.CHOICES, null=True),
     80        db_persist=True,
     81    )
     82
     83    class Meta:
     84        required_db_features = {"supports_stored_generated_columns"}
     85
     86
    7587class Choiceful(models.Model):
    7688    class Suit(models.IntegerChoices):
    7789        DIAMOND = 1, "Diamond"
  • tests/model_fields/tests.py

    diff --git a/tests/model_fields/tests.py b/tests/model_fields/tests.py
    index 36e54d4b8b..8d7caf7072 100644
    a b from .models import (  
    1717    WhizDelayed,
    1818    WhizIter,
    1919    WhizIterEmpty,
     20    WhizGenerated,
    2021)
    2122
    2223
    class GetFieldDisplayTests(SimpleTestCase):  
    297298        self.assertEqual(WhizIterEmpty(c="").c, "")  # Empty value
    298299
    299300
     301class GetFieldDisplayGeneratedTests(TestCase):
     302    def test_choices_and_field_display(self):
     303        wg_0 = WhizGenerated.objects.create(c=0)
     304        wg_1 = WhizGenerated.objects.create(c=1)
     305        wg_none = WhizGenerated.objects.create(c=None)
     306        self.assertEqual(wg_0.get_c_copy_display(), "Other")
     307        self.assertEqual(wg_1.get_c_copy_display(), "First")
     308        self.assertIsNone(wg_none.get_c_display())
     309
     310
    300311class GetChoicesTests(SimpleTestCase):
    301312    def test_empty_choices(self):
    302313        choices = []

comment:2 by Sarah Boyce, 5 months ago

Triage Stage: UnreviewedAccepted

Read through the docs for 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 👍

comment:3 by Ronie Martinez, 5 months ago

Hi Sarah,

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

Thanks for accepting this feature.

in reply to:  3 comment:4 by Sarah Boyce, 5 months ago

Has patch: set
Owner: changed from nobody to Sarah Boyce
Status: newassigned

Replying to Ronie Martinez:

Thanks for accepting this feature.

You're very welcome, thank you for raising 👍

comment:5 by Natalia Bidart, 5 months ago

Patch needs improvement: set

comment:6 by Sarah Boyce, 3 months ago

Patch needs improvement: unset

comment:7 by Timothy Schilling, 6 weeks ago

Cc: Timothy Schilling added
Patch needs improvement: set

comment:8 by Raffaella, 6 weeks ago

Cc: Raffaella added

comment:9 by Sarah Boyce, 6 weeks ago

Resolution: wontfix
Status: assignedclosed

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)

Note: See TracTickets for help on using tickets.
Back to Top