#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.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/01070198a27bfc15-899a6d0a-9f14-4549-a395-b6cb9373d91c-000000%40eu-central-1.amazonses.com.

Reply via email to