Opened 3 months ago
Last modified 5 weeks ago
#35566 assigned New feature
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 | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
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 (6)
comment:1 by , 3 months ago
comment:2 by , 3 months ago
Triage Stage: | Unreviewed → Accepted |
---|
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 👍
follow-up: 4 comment:3 by , 3 months ago
Hi Sarah,
We are getting 'MyModel' object has no attribute 'get_FOO_display'
.
Thanks for accepting this feature.
comment:4 by , 3 months ago
Has patch: | set |
---|---|
Owner: | changed from | to
Status: | new → assigned |
Replying to Ronie Martinez:
Thanks for accepting this feature.
You're very welcome, thank you for raising 👍
comment:5 by , 3 months ago
Patch needs improvement: | set |
---|
comment:6 by , 5 weeks ago
Patch needs improvement: | unset |
---|
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.
ifget_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
tests/model_fields/tests.py