Changed tuple choices to list in docs.

This commit is contained in:
Jon Dufresne 2019-05-15 05:31:42 -07:00 committed by Carlton Gibson
parent 717362d810
commit 97d3321e89
8 changed files with 27 additions and 27 deletions

View File

@ -258,17 +258,17 @@ Model style
* ``def get_absolute_url()`` * ``def get_absolute_url()``
* Any custom methods * Any custom methods
* If ``choices`` is defined for a given model field, define each choice as * If ``choices`` is defined for a given model field, define each choice as a
a tuple of tuples, with an all-uppercase name as a class attribute on the list of tuples, with an all-uppercase name as a class attribute on the model.
model. Example:: Example::
class MyModel(models.Model): class MyModel(models.Model):
DIRECTION_UP = 'U' DIRECTION_UP = 'U'
DIRECTION_DOWN = 'D' DIRECTION_DOWN = 'D'
DIRECTION_CHOICES = ( DIRECTION_CHOICES = [
(DIRECTION_UP, 'Up'), (DIRECTION_UP, 'Up'),
(DIRECTION_DOWN, 'Down'), (DIRECTION_DOWN, 'Down'),
) ]
Use of ``django.conf.settings`` Use of ``django.conf.settings``
=============================== ===============================

View File

@ -47,11 +47,11 @@ simple news application with an ``Article`` model::
from django.db import models from django.db import models
STATUS_CHOICES = ( STATUS_CHOICES = [
('d', 'Draft'), ('d', 'Draft'),
('p', 'Published'), ('p', 'Published'),
('w', 'Withdrawn'), ('w', 'Withdrawn'),
) ]
class Article(models.Model): class Article(models.Model):
title = models.CharField(max_length=100) title = models.CharField(max_length=100)

View File

@ -57,12 +57,12 @@ widget on the field. In the following example, the
from django import forms from django import forms
BIRTH_YEAR_CHOICES = ('1980', '1981', '1982') BIRTH_YEAR_CHOICES = ['1980', '1981', '1982']
FAVORITE_COLORS_CHOICES = ( FAVORITE_COLORS_CHOICES = [
('blue', 'Blue'), ('blue', 'Blue'),
('green', 'Green'), ('green', 'Green'),
('black', 'Black'), ('black', 'Black'),
) ]
class SimpleForm(forms.Form): class SimpleForm(forms.Form):
birth_year = forms.DateField(widget=forms.SelectDateWidget(years=BIRTH_YEAR_CHOICES)) birth_year = forms.DateField(widget=forms.SelectDateWidget(years=BIRTH_YEAR_CHOICES))
@ -90,14 +90,14 @@ changing :attr:`ChoiceField.choices` will update :attr:`Select.choices`. For
example:: example::
>>> from django import forms >>> 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 = forms.ChoiceField(widget=forms.RadioSelect, choices=CHOICES)
>>> choice_field.choices >>> choice_field.choices
[('1', 'First'), ('2', 'Second')] [('1', 'First'), ('2', 'Second')]
>>> choice_field.widget.choices >>> choice_field.widget.choices
[('1', 'First'), ('2', 'Second')] [('1', 'First'), ('2', 'Second')]
>>> choice_field.widget.choices = () >>> choice_field.widget.choices = []
>>> choice_field.choices = (('1', 'First and only',),) >>> choice_field.choices = [('1', 'First and only')]
>>> choice_field.widget.choices >>> choice_field.widget.choices
[('1', 'First and only')] [('1', 'First and only')]

View File

@ -21,11 +21,11 @@ We'll be using the following model in the subsequent examples::
REGULAR = 'R' REGULAR = 'R'
GOLD = 'G' GOLD = 'G'
PLATINUM = 'P' PLATINUM = 'P'
ACCOUNT_TYPE_CHOICES = ( ACCOUNT_TYPE_CHOICES = [
(REGULAR, 'Regular'), (REGULAR, 'Regular'),
(GOLD, 'Gold'), (GOLD, 'Gold'),
(PLATINUM, 'Platinum'), (PLATINUM, 'Platinum'),
) ]
name = models.CharField(max_length=50) name = models.CharField(max_length=50)
registered_on = models.DateField() registered_on = models.DateField()
account_type = models.CharField( account_type = models.CharField(

View File

@ -89,12 +89,12 @@ these choices instead of the standard text field.
The first element in each tuple is the actual value to be set on the model, The first element in each tuple is the actual value to be set on the model,
and the second element is the human-readable name. For example:: and the second element is the human-readable name. For example::
YEAR_IN_SCHOOL_CHOICES = ( YEAR_IN_SCHOOL_CHOICES = [
('FR', 'Freshman'), ('FR', 'Freshman'),
('SO', 'Sophomore'), ('SO', 'Sophomore'),
('JR', 'Junior'), ('JR', 'Junior'),
('SR', 'Senior'), ('SR', 'Senior'),
) ]
Generally, it's best to define choices inside a model class, and to Generally, it's best to define choices inside a model class, and to
define a suitably-named constant for each value:: define a suitably-named constant for each value::
@ -106,12 +106,12 @@ define a suitably-named constant for each value::
SOPHOMORE = 'SO' SOPHOMORE = 'SO'
JUNIOR = 'JR' JUNIOR = 'JR'
SENIOR = 'SR' SENIOR = 'SR'
YEAR_IN_SCHOOL_CHOICES = ( YEAR_IN_SCHOOL_CHOICES = [
(FRESHMAN, 'Freshman'), (FRESHMAN, 'Freshman'),
(SOPHOMORE, 'Sophomore'), (SOPHOMORE, 'Sophomore'),
(JUNIOR, 'Junior'), (JUNIOR, 'Junior'),
(SENIOR, 'Senior'), (SENIOR, 'Senior'),
) ]
year_in_school = models.CharField( year_in_school = models.CharField(
max_length=2, max_length=2,
choices=YEAR_IN_SCHOOL_CHOICES, choices=YEAR_IN_SCHOOL_CHOICES,
@ -130,7 +130,7 @@ will work anywhere that the ``Student`` model has been imported).
You can also collect your available choices into named groups that can You can also collect your available choices into named groups that can
be used for organizational purposes:: be used for organizational purposes::
MEDIA_CHOICES = ( MEDIA_CHOICES = [
('Audio', ( ('Audio', (
('vinyl', 'Vinyl'), ('vinyl', 'Vinyl'),
('cd', 'CD'), ('cd', 'CD'),
@ -142,7 +142,7 @@ be used for organizational purposes::
) )
), ),
('unknown', 'Unknown'), ('unknown', 'Unknown'),
) ]
The first element in each tuple is the name to apply to the group. The The first element in each tuple is the name to apply to the group. The
second element is an iterable of 2-tuples, with each 2-tuple containing second element is an iterable of 2-tuples, with each 2-tuple containing

View File

@ -161,7 +161,7 @@ For example::
class Person(models.Model): class Person(models.Model):
first_name = models.CharField(max_length=50) first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50)
role = models.CharField(max_length=1, choices=(('A', _('Author')), ('E', _('Editor')))) role = models.CharField(max_length=1, choices=[('A', _('Author')), ('E', _('Editor'))])
people = models.Manager() people = models.Manager()
authors = AuthorManager() authors = AuthorManager()
editors = EditorManager() editors = EditorManager()
@ -261,7 +261,7 @@ custom ``QuerySet`` if you also implement them on the ``Manager``::
class Person(models.Model): class Person(models.Model):
first_name = models.CharField(max_length=50) first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50)
role = models.CharField(max_length=1, choices=(('A', _('Author')), ('E', _('Editor')))) role = models.CharField(max_length=1, choices=[('A', _('Author')), ('E', _('Editor'))])
people = PersonManager() people = PersonManager()
This example allows you to call both ``authors()`` and ``editors()`` directly from This example allows you to call both ``authors()`` and ``editors()`` directly from

View File

@ -161,13 +161,13 @@ ones:
A choices list looks like this:: A choices list looks like this::
YEAR_IN_SCHOOL_CHOICES = ( YEAR_IN_SCHOOL_CHOICES = [
('FR', 'Freshman'), ('FR', 'Freshman'),
('SO', 'Sophomore'), ('SO', 'Sophomore'),
('JR', 'Junior'), ('JR', 'Junior'),
('SR', 'Senior'), ('SR', 'Senior'),
('GR', 'Graduate'), ('GR', 'Graduate'),
) ]
The first element in each tuple is the value that will be stored in the The first element in each tuple is the value that will be stored in the
database. The second element is displayed by the field's form widget. database. The second element is displayed by the field's form widget.

View File

@ -165,11 +165,11 @@ Consider this set of models::
from django.db import models from django.db import models
from django.forms import ModelForm from django.forms import ModelForm
TITLE_CHOICES = ( TITLE_CHOICES = [
('MR', 'Mr.'), ('MR', 'Mr.'),
('MRS', 'Mrs.'), ('MRS', 'Mrs.'),
('MS', 'Ms.'), ('MS', 'Ms.'),
) ]
class Author(models.Model): class Author(models.Model):
name = models.CharField(max_length=100) name = models.CharField(max_length=100)