#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>
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 django-updates+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/django-updates/01070198a29ed715-1c6fc940-72d4-4c5b-8aa2-6266aa562efa-000000%40eu-central-1.amazonses.com.

Reply via email to