Opened 6 months ago
Closed 2 months 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 , 6 months ago
comment:2 by , 6 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 , 6 months ago
Hi Sarah,
We are getting 'MyModel' object has no attribute 'get_FOO_display'
.
Thanks for accepting this feature.
comment:4 by , 6 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 , 6 months ago
Patch needs improvement: | set |
---|
comment:6 by , 4 months ago
Patch needs improvement: | unset |
---|
comment:7 by , 2 months ago
Cc: | added |
---|---|
Patch needs improvement: | set |
comment:8 by , 2 months ago
Cc: | added |
---|
comment:9 by , 2 months ago
Resolution: | → wontfix |
---|---|
Status: | assigned → closed |
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)
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