diff --git a/docs/howto/custom-model-fields.txt b/docs/howto/custom-model-fields.txt index 5c2fed3b34..f1dd9dc56c 100644 --- a/docs/howto/custom-model-fields.txt +++ b/docs/howto/custom-model-fields.txt @@ -334,7 +334,6 @@ Once you have ``MytypeField``, you can use it in any model, just like any other class Person(models.Model): name = models.CharField(max_length=80) - gender = models.CharField(max_length=1) something_else = MytypeField() If you aim to build a database-agnostic application, you should account for diff --git a/docs/ref/forms/widgets.txt b/docs/ref/forms/widgets.txt index ac110c2ee8..88d0d706cd 100644 --- a/docs/ref/forms/widgets.txt +++ b/docs/ref/forms/widgets.txt @@ -47,14 +47,12 @@ widget on the field. In the following example, the from django.forms.extras.widgets import SelectDateWidget BIRTH_YEAR_CHOICES = ('1980', '1981', '1982') - GENDER_CHOICES = (('m', 'Male'), ('f', 'Female')) FAVORITE_COLORS_CHOICES = (('blue', 'Blue'), ('green', 'Green'), ('black', 'Black')) class SimpleForm(forms.Form): birth_year = DateField(widget=SelectDateWidget(years=BIRTH_YEAR_CHOICES)) - gender = ChoiceField(widget=RadioSelect, choices=GENDER_CHOICES) favorite_colors = forms.MultipleChoiceField(required=False, widget=CheckboxSelectMultiple, choices=FAVORITE_COLORS_CHOICES) diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt index 86894effea..629efc0432 100644 --- a/docs/ref/models/fields.txt +++ b/docs/ref/models/fields.txt @@ -102,20 +102,26 @@ element is the human-readable name for the option. The choices list can be defined either as part of your model class:: class Foo(models.Model): - GENDER_CHOICES = ( - ('M', 'Male'), - ('F', 'Female'), + YEAR_IN_SCHOOL_CHOICES = ( + ('FR', 'Freshman'), + ('SO', 'Sophomore'), + ('JR', 'Junior'), + ('SR', 'Senior'), + ('GR', 'Graduate'), ) - gender = models.CharField(max_length=1, choices=GENDER_CHOICES) + year_in_school = models.CharField(max_length=2, choices=YEAR_IN_SCHOOL_CHOICES) or outside your model class altogether:: - GENDER_CHOICES = ( - ('M', 'Male'), - ('F', 'Female'), + YEAR_IN_SCHOOL_CHOICES = ( + ('FR', 'Freshman'), + ('SO', 'Sophomore'), + ('JR', 'Junior'), + ('SR', 'Senior'), + ('GR', 'Graduate'), ) class Foo(models.Model): - gender = models.CharField(max_length=1, choices=GENDER_CHOICES) + 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 be used for organizational purposes:: diff --git a/docs/ref/models/instances.txt b/docs/ref/models/instances.txt index fb0fcc046d..50fb085d38 100644 --- a/docs/ref/models/instances.txt +++ b/docs/ref/models/instances.txt @@ -572,25 +572,29 @@ might have some of the following methods: For every field that has :attr:`~django.db.models.Field.choices` set, the object will have a ``get_FOO_display()`` method, where ``FOO`` is the name of -the field. This method returns the "human-readable" value of the field. For -example, in the following model:: +the field. This method returns the "human-readable" value of the field. - GENDER_CHOICES = ( - ('M', 'Male'), - ('F', 'Female'), - ) - class Person(models.Model): - name = models.CharField(max_length=20) - gender = models.CharField(max_length=1, choices=GENDER_CHOICES) +For example:: -...each ``Person`` instance will have a ``get_gender_display()`` method. Example:: + from django.db import models - >>> p = Person(name='John', gender='M') - >>> p.save() - >>> p.gender - 'M' - >>> p.get_gender_display() - 'Male' + class Person(models.Model): + SHIRT_SIZES = ( + (u'S', u'Small'), + (u'M', u'Medium'), + (u'L', u'Large'), + ) + name = models.CharField(max_length=60) + shirt_size = models.CharField(max_length=2, choices=SHIRT_SIZES) + + :: + + >>> p = Person(name="Fred Flintstone", shirt_size="L") + >>> p.save() + >>> p.shirt_size + u'L' + >>> p.get_shirt_size_display() + u'Large' .. method:: Model.get_next_by_FOO(\**kwargs) .. method:: Model.get_previous_by_FOO(\**kwargs) diff --git a/docs/ref/templates/builtins.txt b/docs/ref/templates/builtins.txt index bc86cb6919..805aaf7f6f 100644 --- a/docs/ref/templates/builtins.txt +++ b/docs/ref/templates/builtins.txt @@ -771,48 +771,41 @@ regroup Regroups a list of alike objects by a common attribute. -This complex tag is best illustrated by use of an example: say that ``people`` -is a list of people represented by dictionaries with ``first_name``, -``last_name``, and ``gender`` keys: +This complex tag is best illustrated by way of an example: say that "places" is a list of cities represented by dictionaries containing ``"name"``, ``"population"``, and ``"country"`` keys: .. code-block:: python - people = [ - {'first_name': 'George', 'last_name': 'Bush', 'gender': 'Male'}, - {'first_name': 'Bill', 'last_name': 'Clinton', 'gender': 'Male'}, - {'first_name': 'Margaret', 'last_name': 'Thatcher', 'gender': 'Female'}, - {'first_name': 'Condoleezza', 'last_name': 'Rice', 'gender': 'Female'}, - {'first_name': 'Pat', 'last_name': 'Smith', 'gender': 'Unknown'}, + cities = [ + {'name': 'Mumbai', 'population': '19,000,000', 'country': 'India'}, + {'name': 'Calcutta', 'population': '15,000,000', 'country': 'India'}, + {'name': 'New York', 'population': '20,000,000', 'country': 'USA'}, + {'name': 'Chicago', 'population': '7,000,000', 'country': 'USA'}, + {'name': 'Tokyo', 'population': '33,000,000', 'country': 'Japan'}, ] -...and you'd like to display a hierarchical list that is ordered by gender, -like this: +...and you'd like to display a hierarchical list that is ordered by country, like this: -* Male: +* India + * Mumbai: 19,000,000 + * Calcutta: 15,000,000 +* USA + * New York: 20,000,000 + * Chicago: 7,000,000 +* Japan + * Tokyo: 33,000,000 - * George Bush - * Bill Clinton -* Female: - - * Margaret Thatcher - * Condoleezza Rice - -* Unknown: - - * Pat Smith - -You can use the ``{% regroup %}`` tag to group the list of people by gender. +You can use the ``{% regroup %}`` tag to group the list of cities by country. The following snippet of template code would accomplish this:: - {% regroup people by gender as gender_list %} + {% regroup cities by country as country_list %}