#34899: Model Field.choices callable support is not actually lazy
-------------------------------------+-------------------------------------
     Reporter:  Adam Johnson         |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  5.0
  (models, ORM)                      |
     Severity:  Release blocker      |               Resolution:
     Keywords:                       |             Triage Stage:  Accepted
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------

Comment (by apoorvaeternity):

 Here
 
[https://github.com/django/django/blob/f6629ee2c986d3bf59b4c1b3058f370a00bdc573/django/db/models/fields/__init__.py#L1115]
 if we add a check for `CallableChoiceIterator` and set
 `defaults["choices"]` as the `choices callable` instead of calling the
 `get_choices` method directly, that seems to fix this issue.

 {{{#!python
 if isinstance(self.choices, CallableChoiceIterator):
     defaults["choices"] = self.choices
 else:
     include_blank = self.blank or not (
         self.has_default() or "initial" in kwargs
     )
     defaults["choices"] = self.get_choices(include_blank=include_blank)
 }}}

 The only caveat with this solution is that the blank choice has to be set
 within the `choices callable` and would no longer be handled by
 `get_choices` as it relies on checking if a blank choice exists as one of
 the available choices which itself requires calling the `choices
 callable`.

 I have tested this using the example posted in the issue description and
 it works. If this solution looks good I would be happy to land a PR.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/34899#comment:3>
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 on the web visit 
https://groups.google.com/d/msgid/django-updates/0107018b38c649e2-4b647cf8-f5a1-49e5-9484-034c592fa92a-000000%40eu-central-1.amazonses.com.

Reply via email to