Pull Request: https://github.com/django/django/pull/4434/
--
Ticket URL: <https://code.djangoproject.com/ticket/24561>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 1
* component: Core (System checks) => Database layer (models, ORM)
* needs_tests: => 0
* version: 1.8 => master
* needs_docs: => 0
* type: Bug => New feature
* stage: Unreviewed => Accepted
Comment:
It was added for `django.forms.ChoiceField`, but not model field
`choices`. It might be a reasonable feature request, but it needs to be
implemented. It might cause some difficulty in migrations, but I will
accept the ticket if someone wants to investigate the possibility.
--
Ticket URL: <https://code.djangoproject.com/ticket/24561#comment:1>
Comment (by EnTeQuAk):
Oh, then I misread the feature. I'll try to update the patch to port this
to the model field `choices` as well.
--
Ticket URL: <https://code.djangoproject.com/ticket/24561#comment:2>
* needs_better_patch: 1 => 0
* has_patch: 1 => 0
Comment:
See also discussion in #28033.
--
Ticket URL: <https://code.djangoproject.com/ticket/24561#comment:3>
* cc: Sergey Fedoseev (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/24561#comment:4>
* status: new => assigned
* owner: nobody => Sergey Fedoseev
--
Ticket URL: <https://code.djangoproject.com/ticket/24561#comment:5>
Comment (by Natalia Bidart):
While reviewing/working on #31262, we noticed that the choices refactoring
that is being proposed in [https://github.com/django/django/pull/16943 PR
#16943] will help considerably to this ticket. Besides what is discussed
in that PR, the following diff should allow model field's callable
`choices` to be serialized as functions (similar to callable `default` or
form ChoiceField's `choices`).
{{{
#!diff
diff --git a/django/db/migrations/serializer.py
b/django/db/migrations/serializer.py
index 06657ebaab..2c57a21b7c 100644
--- a/django/db/migrations/serializer.py
+++ b/django/db/migrations/serializer.py
@@ -15,6 +15,7 @@ from django.conf import SettingsReference
from django.db import models
from django.db.migrations.operations.base import Operation
from django.db.migrations.utils import COMPILED_REGEX_TYPE, RegexObject
+from django.utils.choices import CallableChoiceIterator
from django.utils.functional import LazyObject, Promise
from django.utils.version import PY311, get_docs_version
@@ -329,6 +330,12 @@ class UUIDSerializer(BaseSerializer):
return "uuid.%s" % repr(self.value), {"import uuid"}
+class CallableChoiceIteratorSerializer(BaseSerializer):
+
+ def serialize(self):
+ return
FunctionTypeSerializer(self.value.choices_func).serialize()
+
+
class Serializer:
_registry = {
# Some of these are order-dependent.
@@ -351,6 +358,7 @@ class Serializer:
types.BuiltinFunctionType,
types.MethodType,
): FunctionTypeSerializer,
+ CallableChoiceIterator: CallableChoiceIteratorSerializer,
collections.abc.Iterable: IterableSerializer,
(COMPILED_REGEX_TYPE, RegexObject): RegexSerializer,
uuid.UUID: UUIDSerializer,
diff --git a/django/db/models/fields/__init__.py
b/django/db/models/fields/__init__.py
index 7e56bddffe..6600031cf6 100644
--- a/django/db/models/fields/__init__.py
+++ b/django/db/models/fields/__init__.py
@@ -315,9 +315,7 @@ class Field(RegisterLookupMixin):
if not self.choices:
return []
- if not is_iterable(self.choices) or isinstance(
- self.choices, (str, CallableChoiceIterator)
- ):
+ if not is_iterable(self.choices) or isinstance(self.choices,
str):
return [
checks.Error(
"'choices' must be a mapping (e.g. a dictionary) or
an iterable "
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/24561#comment:5>
* cc: Sergey Fedoseev (removed)
--
Ticket URL: <https://code.djangoproject.com/ticket/24561#comment:6>
* owner: nobody => Natalia Bidart
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/24561#comment:7>
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/17214 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/24561#comment:8>
* needs_better_patch: 0 => 1
* needs_docs: 0 => 1
Comment:
Setting as patch needs improvement because I'd like to cover a few use
cases with the community first.
--
Ticket URL: <https://code.djangoproject.com/ticket/24561#comment:9>
* cc: Ülgen Sarıkavak (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/24561#comment:10>
Comment (by Natalia Bidart):
[https://forum.djangoproject.com/t/callables-for-model-fields-
choices/23654 Forum Post]
--
Ticket URL: <https://code.djangoproject.com/ticket/24561#comment:11>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"691f70c47755c7f55fa75ce607d028f81468b745" 691f70c]:
{{{
#!CommitTicketReference repository=""
revision="691f70c47755c7f55fa75ce607d028f81468b745"
Fixed #24561 -- Added support for callables on model fields' choices.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/24561#comment:12>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/24561#comment:13>