﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
36548	"GeneratedField ignores it's output field ""choices"" argument"	Olivier Dalang		"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'
}}}
"	Bug	closed	Database layer (models, ORM)	5.2	Normal	invalid			Unreviewed	0	0	0	0	0	0
