Fixed #31262 -- Added support for mappings on model fields and ChoiceField's choices.

This commit is contained in:
Nick Pope 2023-08-31 02:57:40 +01:00 committed by GitHub
parent 68a8996bdf
commit 500e01073a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 822 additions and 249 deletions

View file

@ -298,16 +298,23 @@ 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.
mapping, 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:`field-choices-enum-types`::
class MyModel(models.Model):
class Direction(models.TextChoices):
UP = U, "Up"
DOWN = D, "Down"
Use of ``django.conf.settings``
===============================