Skip to content

Commit

Permalink
fixup! Fixed #31262 -- Allowed dictionaries in Field.choices for name…
Browse files Browse the repository at this point in the history
…d groups.
  • Loading branch information
ngnpope committed Jun 11, 2023
1 parent d2b124c commit 2897fc7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
18 changes: 13 additions & 5 deletions docs/internals/contributing/writing-code/coding-style.txt
Expand Up @@ -298,16 +298,24 @@ Model style
* Any custom methods

* If ``choices`` is defined for a given model field, define each choice as a
list of tuples, with an all-uppercase name as a class attribute on the model.
dictionary, with an all-uppercase name as a class attribute on the model.
Example::

class MyModel(models.Model):
DIRECTION_UP = "U"
DIRECTION_DOWN = "D"
DIRECTION_CHOICES = [
(DIRECTION_UP, "Up"),
(DIRECTION_DOWN, "Down"),
]
DIRECTION_CHOICES = {
DIRECTION_UP: "Up",
DIRECTION_DOWN: "Down",
}

Alternatively, consider using :ref:`enumeration types
<field-choices-enum-types>`::

class MyModel(models.Model):
class Direction(models.TextChoices):
UP = U, "Up"
DOWN = D, "Down"

Use of ``django.conf.settings``
===============================
Expand Down
2 changes: 1 addition & 1 deletion docs/ref/forms/widgets.txt
Expand Up @@ -95,7 +95,7 @@ example:
.. code-block:: pycon

>>> from django import forms
>>> CHOICES = [("1", "First"), ("2", "Second")]
>>> CHOICES = {"1": "First", "2": "Second"}
>>> choice_field = forms.ChoiceField(widget=forms.RadioSelect, choices=CHOICES)
>>> choice_field.choices
[('1', 'First'), ('2', 'Second')]
Expand Down

0 comments on commit 2897fc7

Please sign in to comment.