mirror of
https://github.com/django/django.git
synced 2024-12-27 03:25:58 +00:00
Merge pull request #256 from ubernostrum/model-choice-docs
Fix #18062: Document best practices for choices in model fields.
This commit is contained in:
commit
b1517a310a
@ -86,42 +86,43 @@ field.
|
|||||||
If this is given, Django's admin will use a select box instead of the standard
|
If this is given, Django's admin will use a select box instead of the standard
|
||||||
text field and will limit choices to the choices given.
|
text field and will limit choices to the choices given.
|
||||||
|
|
||||||
A choices list looks like this::
|
A choices list is an iterable of 2-tuples; the first element in each
|
||||||
|
tuple is the actual value to be stored, 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'),
|
||||||
('GR', 'Graduate'),
|
|
||||||
)
|
)
|
||||||
|
|
||||||
The first element in each tuple is the actual value to be stored. The second
|
Generally, it's best to define choices inside a model class, and to
|
||||||
element is the human-readable name for the option.
|
define a suitably-named constant for each value::
|
||||||
|
|
||||||
The choices list can be defined either as part of your model class::
|
class Student(models.Model):
|
||||||
|
FRESHMAN = 'FR'
|
||||||
class Foo(models.Model):
|
SOPHOMORE = 'SO'
|
||||||
|
JUNIOR = 'JR'
|
||||||
|
SENIOR = 'SR'
|
||||||
YEAR_IN_SCHOOL_CHOICES = (
|
YEAR_IN_SCHOOL_CHOICES = (
|
||||||
('FR', 'Freshman'),
|
(FRESHMAN, 'Freshman'),
|
||||||
('SO', 'Sophomore'),
|
(SOPHOMORE, 'Sophomore'),
|
||||||
('JR', 'Junior'),
|
(JUNIOR, 'Junior'),
|
||||||
('SR', 'Senior'),
|
(SENIOR, 'Senior'),
|
||||||
('GR', 'Graduate'),
|
|
||||||
)
|
)
|
||||||
year_in_school = models.CharField(max_length=2, choices=YEAR_IN_SCHOOL_CHOICES)
|
year_in_school = models.CharField(max_length=2,
|
||||||
|
choices=YEAR_IN_SCHOOL_CHOICES,
|
||||||
|
default=FRESHMAN)
|
||||||
|
|
||||||
or outside your model class altogether::
|
def is_upperclass(self):
|
||||||
|
return self.year_in_school in (self.JUNIOR, self.SENIOR)
|
||||||
|
|
||||||
YEAR_IN_SCHOOL_CHOICES = (
|
Though you can define a choices list outside of a model class and then
|
||||||
('FR', 'Freshman'),
|
refer to it, defining the choices and names for each choice inside the
|
||||||
('SO', 'Sophomore'),
|
model class keeps all of that information with the class that uses it,
|
||||||
('JR', 'Junior'),
|
and makes the choices easy to reference (e.g, ``Student.SOPHOMORE``
|
||||||
('SR', 'Senior'),
|
will work anywhere that the ``Student`` model has been imported).
|
||||||
('GR', 'Graduate'),
|
|
||||||
)
|
|
||||||
class Foo(models.Model):
|
|
||||||
year_in_school = models.CharField(max_length=2, choices=YEAR_IN_SCHOOL_CHOICES)
|
|
||||||
|
|
||||||
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::
|
||||||
|
Loading…
Reference in New Issue
Block a user