[Django] #24561: Allow 'choices'-check to work for callables.

8 views
Skip to first unread message

Django

unread,
Apr 2, 2015, 12:49:00 AM4/2/15
to django-...@googlegroups.com
#24561: Allow 'choices'-check to work for callables.
--------------------------------------+--------------------
Reporter: EnTeQuAk | Owner: nobody
Type: Bug | Status: new
Component: Core (System checks) | Version: 1.8
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Easy pickings: 0 | UI/UX: 0
--------------------------------------+--------------------
Django 1.8 introduced the ability to pass a callable for 'choices' but it
seems the check-framework was not adapted to this.

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.

Django

unread,
Apr 2, 2015, 6:33:01 AM4/2/15
to django-...@googlegroups.com
#24561: Allow model field choices to accept callables.
-------------------------------------+-------------------------------------
Reporter: EnTeQuAk | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* 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>

Django

unread,
Apr 2, 2015, 6:48:38 AM4/2/15
to django-...@googlegroups.com
#24561: Allow model field choices to accept callables.
-------------------------------------+-------------------------------------
Reporter: EnTeQuAk | Owner: nobody

Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Apr 20, 2017, 4:30:25 PM4/20/17
to django-...@googlegroups.com
#24561: Allow model field choices to accept callables.
-------------------------------------+-------------------------------------
Reporter: Christopher Grebs | Owner: nobody

Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* 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>

Django

unread,
Oct 25, 2017, 2:26:46 AM10/25/17
to django-...@googlegroups.com
#24561: Allow model field choices to accept callables.
-------------------------------------+-------------------------------------
Reporter: Christopher Grebs | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sergey Fedoseev):

* cc: Sergey Fedoseev (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/24561#comment:4>

Django

unread,
Nov 10, 2017, 8:08:38 AM11/10/17
to django-...@googlegroups.com
#24561: Allow model field choices to accept callables.
-------------------------------------+-------------------------------------
Reporter: Christopher Grebs | Owner: Sergey
| Fedoseev
Type: New feature | Status: assigned

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sergey Fedoseev):

* status: new => assigned
* owner: nobody => Sergey Fedoseev


--
Ticket URL: <https://code.djangoproject.com/ticket/24561#comment:5>

Django

unread,
Aug 24, 2023, 3:48:06 PM8/24/23
to django-...@googlegroups.com
#24561: Allow model field choices to accept callables.
-------------------------------------+-------------------------------------
Reporter: Christopher Grebs | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: dev

(models, ORM) |
Severity: Normal | 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 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 "
}}}

Django

unread,
Aug 26, 2023, 2:26:15 AM8/26/23
to django-...@googlegroups.com
#24561: Allow model field choices to accept callables.
-------------------------------------+-------------------------------------
Reporter: Christopher Grebs | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Sergey Fedoseev):

* cc: Sergey Fedoseev (removed)


--
Ticket URL: <https://code.djangoproject.com/ticket/24561#comment:6>

Django

unread,
Aug 30, 2023, 10:03:46 PM8/30/23
to django-...@googlegroups.com
#24561: Allow model field choices to accept callables.
-------------------------------------+-------------------------------------
Reporter: Christopher Grebs | Owner: Natalia
| Bidart

Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* owner: nobody => Natalia Bidart


* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/24561#comment:7>

Django

unread,
Aug 31, 2023, 9:33:03 PM8/31/23
to django-...@googlegroups.com
#24561: Allow model field choices to accept callables.
-------------------------------------+-------------------------------------
Reporter: Christopher Grebs | Owner: Natalia
| Bidart
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* has_patch: 0 => 1


Comment:

[https://github.com/django/django/pull/17214 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/24561#comment:8>

Django

unread,
Sep 4, 2023, 10:46:40 AM9/4/23
to django-...@googlegroups.com
#24561: Allow model field choices to accept callables.
-------------------------------------+-------------------------------------
Reporter: Christopher Grebs | Owner: Natalia
| Bidart
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* 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>

Django

unread,
Sep 4, 2023, 12:38:19 PM9/4/23
to django-...@googlegroups.com
#24561: Allow model field choices to accept callables.
-------------------------------------+-------------------------------------
Reporter: Christopher Grebs | Owner: Natalia
| Bidart
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Ülgen Sarıkavak):

* cc: Ülgen Sarıkavak (added)


--
Ticket URL: <https://code.djangoproject.com/ticket/24561#comment:10>

Django

unread,
Sep 6, 2023, 8:39:36 AM9/6/23
to django-...@googlegroups.com
#24561: Allow model field choices to accept callables.
-------------------------------------+-------------------------------------
Reporter: Christopher Grebs | Owner: Natalia
| Bidart
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Sep 14, 2023, 10:11:06 AM9/14/23
to django-...@googlegroups.com
#24561: Allow model field choices to accept callables.
-------------------------------------+-------------------------------------
Reporter: Christopher Grebs | Owner: Natalia
| Bidart
Type: New feature | Status: closed

Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia <124304+nessita@…>):

* 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>

Django

unread,
Sep 14, 2023, 12:08:09 PM9/14/23
to django-...@googlegroups.com
#24561: Allow model field choices to accept callables.
-------------------------------------+-------------------------------------
Reporter: Christopher Grebs | Owner: Natalia
| Bidart
Type: New feature | Status: closed
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Natalia Bidart):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/24561#comment:13>

Reply all
Reply to author
Forward
0 new messages