[Django] #36548: GeneratedField ignores it's output field "choices" argument

2 views
Skip to first unread message

Django

unread,
Aug 13, 2025, 4:11:39 AMAug 13
to django-...@googlegroups.com
#36548: GeneratedField ignores it's output field "choices" argument
--------------------------------+-----------------------------------------
Reporter: Olivier Dalang | Type: Uncategorized
Status: new | Component: Uncategorized
Version: 5.2 | Severity: Normal
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
--------------------------------+-----------------------------------------
Hello !

GeneratedFields take the "output_field" argument to define the field's
data type. One can set a CharField with the "choices" argument as output
type.

Unfortunately, the resulting field doesn't behave as a regular CharField
with the choices argument (it seems the "choices" is just ignored).

I don't know all internal implications around GeneratedField and
output_type, but naively I would expect this to work, just as I expect
GeneratedFields of type DateField to output actual python dates. This not
working makes it difficult to transition from/to GeneratedFields in
migration scenarios, prevents nice integration in django admin (such as
labels for filters), and generally requires boilerplate code to map back
to the display value.

Below is a small example to reproduce the issue.

Cheers !!

Olivier


{{{#!python
class YearInSchool(models.TextChoices):
FRESHMAN = "FR", "Freshman"
SOPHOMORE = "SO", "Sophomore"

class MyModel(models.Model):
normal_choice = models.CharField(
choices=YearInSchool, default=YearInSchool.FRESHMAN
)
generated_choice = models.GeneratedField(
expression=models.F("normal_choice"),
output_field=models.CharField(choices=YearInSchool),
db_persist=False,
)
}}}

{{{#!python
myobj = MyModel.objects.create()

myobj.get_normal_choice_display() # works as expected
# 'Freshman'

myobj.get_generated_choice_display() # oh no, non of these choices
goodies :-(
# AttributeError: 'MyModel' object has no attribute
'get_generated_choice_display'.

myobj.normal_choice # works as expected
# YearInSchool.FRESHMAN

myobj.generated_choice # we get the raw string
# 'FR'
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36548>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 13, 2025, 4:49:42 AMAug 13
to django-...@googlegroups.com
#36548: GeneratedField ignores it's output field "choices" argument
-------------------------------------+-------------------------------------
Reporter: Olivier Dalang | Owner: (none)
Type: Bug | Status: closed
Component: Database layer | Version: 5.2
(models, ORM) |
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sarah Boyce):

* component: Uncategorized => Database layer (models, ORM)
* resolution: => invalid
* status: new => closed
* type: Uncategorized => Bug

Comment:

Similar to #35566, `choices` needs to be set on `GeneratedField` so
{{{#!python
generated_choice = models.GeneratedField(
expression=models.F("normal_choice"),
output_field=models.CharField(choices=YearInSchool),
db_persist=False,
choices=YearInSchool,
)
}}}
In which case:
{{{#!python
myobj = MyModel.objects.create()
myobj.get_normal_choice_display()
# 'Freshman'
myobj.get_generated_choice_display()
# 'Freshman'
myobj.normal_choice # driven by the default value which wouldn't make
sense for the GeneratedField
# YearInSchool.FRESHMAN
myobj.generated_choice
# 'FR'

myobj.refresh_from_db()
myobj.normal_choice
# 'FR'
myobj.generated_choice
# 'FR'
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36548#comment:1>
Reply all
Reply to author
Forward
0 new messages