Skip to content

Commit

Permalink
[1.4.X] Replaced documentation snippets using "gender" with less sens…
Browse files Browse the repository at this point in the history
…itive examples.

Backport of [7edf231] from master.
  • Loading branch information
jacobian committed Jun 6, 2012
1 parent 0a8a6b9 commit 45d4331
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 110 deletions.
1 change: 0 additions & 1 deletion docs/howto/custom-model-fields.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -334,7 +334,6 @@ Once you have ``MytypeField``, you can use it in any model, just like any other


class Person(models.Model): class Person(models.Model):
name = models.CharField(max_length=80) name = models.CharField(max_length=80)
gender = models.CharField(max_length=1)
something_else = MytypeField() something_else = MytypeField()


If you aim to build a database-agnostic application, you should account for If you aim to build a database-agnostic application, you should account for
Expand Down
2 changes: 0 additions & 2 deletions docs/ref/forms/widgets.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -47,14 +47,12 @@ widget on the field. In the following example, the
from django.forms.extras.widgets import SelectDateWidget from django.forms.extras.widgets import SelectDateWidget


BIRTH_YEAR_CHOICES = ('1980', '1981', '1982') BIRTH_YEAR_CHOICES = ('1980', '1981', '1982')
GENDER_CHOICES = (('m', 'Male'), ('f', 'Female'))
FAVORITE_COLORS_CHOICES = (('blue', 'Blue'), FAVORITE_COLORS_CHOICES = (('blue', 'Blue'),
('green', 'Green'), ('green', 'Green'),
('black', 'Black')) ('black', 'Black'))


class SimpleForm(forms.Form): class SimpleForm(forms.Form):
birth_year = DateField(widget=SelectDateWidget(years=BIRTH_YEAR_CHOICES)) birth_year = DateField(widget=SelectDateWidget(years=BIRTH_YEAR_CHOICES))
gender = ChoiceField(widget=RadioSelect, choices=GENDER_CHOICES)
favorite_colors = forms.MultipleChoiceField(required=False, favorite_colors = forms.MultipleChoiceField(required=False,
widget=CheckboxSelectMultiple, choices=FAVORITE_COLORS_CHOICES) widget=CheckboxSelectMultiple, choices=FAVORITE_COLORS_CHOICES)


Expand Down
22 changes: 14 additions & 8 deletions docs/ref/models/fields.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -103,20 +103,26 @@ element is the human-readable name for the option.
The choices list can be defined either as part of your model class:: The choices list can be defined either as part of your model class::


class Foo(models.Model): class Foo(models.Model):
GENDER_CHOICES = ( YEAR_IN_SCHOOL_CHOICES = (
('M', 'Male'), ('FR', 'Freshman'),
('F', 'Female'), ('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:: or outside your model class altogether::


GENDER_CHOICES = ( YEAR_IN_SCHOOL_CHOICES = (
('M', 'Male'), ('FR', 'Freshman'),
('F', 'Female'), ('SO', 'Sophomore'),
('JR', 'Junior'),
('SR', 'Senior'),
('GR', 'Graduate'),
) )
class Foo(models.Model): 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 You can also collect your available choices into named groups that can
be used for organizational purposes:: be used for organizational purposes::
Expand Down
38 changes: 21 additions & 17 deletions docs/ref/models/instances.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -550,25 +550,29 @@ might have some of the following methods:


For every field that has :attr:`~django.db.models.Field.choices` set, the 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 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 the field. This method returns the "human-readable" value of the field.
example, in the following model::


GENDER_CHOICES = ( For example::
('M', 'Male'),
('F', 'Female'),
)
class Person(models.Model):
name = models.CharField(max_length=20)
gender = models.CharField(max_length=1, choices=GENDER_CHOICES)

...each ``Person`` instance will have a ``get_gender_display()`` method. Example::


>>> p = Person(name='John', gender='M') from django.db import models
>>> p.save()
>>> p.gender class Person(models.Model):
'M' SHIRT_SIZES = (
>>> p.get_gender_display() (u'S', u'Small'),
'Male' (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_next_by_FOO(\**kwargs)
.. method:: Model.get_previous_by_FOO(\**kwargs) .. method:: Model.get_previous_by_FOO(\**kwargs)
Expand Down
127 changes: 54 additions & 73 deletions docs/ref/templates/builtins.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -771,48 +771,41 @@ regroup


Regroups a list of alike objects by a common attribute. Regroups a list of alike objects by a common attribute.


This complex tag is best illustrated by use of an example: say that ``people`` 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:
is a list of people represented by dictionaries with ``first_name``,
``last_name``, and ``gender`` keys:


.. code-block:: python .. code-block:: python


people = [ cities = [
{'first_name': 'George', 'last_name': 'Bush', 'gender': 'Male'}, {'name': 'Mumbai', 'population': '19,000,000', 'country': 'India'},
{'first_name': 'Bill', 'last_name': 'Clinton', 'gender': 'Male'}, {'name': 'Calcutta', 'population': '15,000,000', 'country': 'India'},
{'first_name': 'Margaret', 'last_name': 'Thatcher', 'gender': 'Female'}, {'name': 'New York', 'population': '20,000,000', 'country': 'USA'},
{'first_name': 'Condoleezza', 'last_name': 'Rice', 'gender': 'Female'}, {'name': 'Chicago', 'population': '7,000,000', 'country': 'USA'},
{'first_name': 'Pat', 'last_name': 'Smith', 'gender': 'Unknown'}, {'name': 'Tokyo', 'population': '33,000,000', 'country': 'Japan'},
] ]


...and you'd like to display a hierarchical list that is ordered by gender, ...and you'd like to display a hierarchical list that is ordered by country, like this:
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: You can use the ``{% regroup %}`` tag to group the list of cities by country.

* Margaret Thatcher
* Condoleezza Rice

* Unknown:

* Pat Smith

You can use the ``{% regroup %}`` tag to group the list of people by gender.
The following snippet of template code would accomplish this:: The following snippet of template code would accomplish this::


{% regroup people by gender as gender_list %} {% regroup cities by country as country_list %}


<ul> <ul>
{% for gender in gender_list %} {% for country in country_list %}
<li>{{ gender.grouper }} <li>{{ country.grouper }}
<ul> <ul>
{% for item in gender.list %} {% for item in country.list %}
<li>{{ item.first_name }} {{ item.last_name }}</li> <li>{{ item.name }}: {{ item.population }}</li>
{% endfor %} {% endfor %}
</ul> </ul>
</li> </li>
Expand All @@ -821,84 +814,72 @@ The following snippet of template code would accomplish this::


Let's walk through this example. ``{% regroup %}`` takes three arguments: the Let's walk through this example. ``{% regroup %}`` takes three arguments: the
list you want to regroup, the attribute to group by, and the name of the list you want to regroup, the attribute to group by, and the name of the
resulting list. Here, we're regrouping the ``people`` list by the ``gender`` resulting list. Here, we're regrouping the ``cities`` list by the ``country``
attribute and calling the result ``gender_list``. attribute and calling the result ``country_list``.


``{% regroup %}`` produces a list (in this case, ``gender_list``) of ``{% regroup %}`` produces a list (in this case, ``country_list``) of
**group objects**. Each group object has two attributes: **group objects**. Each group object has two attributes:


* ``grouper`` -- the item that was grouped by (e.g., the string "Male" or * ``grouper`` -- the item that was grouped by (e.g., the string "India" or
"Female"). "Japan").
* ``list`` -- a list of all items in this group (e.g., a list of all people * ``list`` -- a list of all items in this group (e.g., a list of all cities
with gender='Male'). with country='India').


Note that ``{% regroup %}`` does not order its input! Our example relies on Note that ``{% regroup %}`` does not order its input! Our example relies on
the fact that the ``people`` list was ordered by ``gender`` in the first place. the fact that the ``cities`` list was ordered by ``country`` in the first place.
If the ``people`` list did *not* order its members by ``gender``, the If the ``cities`` list did *not* order its members by ``country``, the
regrouping would naively display more than one group for a single gender. For regrouping would naively display more than one group for a single country. For
example, say the ``people`` list was set to this (note that the males are not example, say the ``cities`` list was set to this (note that the countries are not
grouped together): grouped together):


.. code-block:: python .. code-block:: python


people = [ cities = [
{'first_name': 'Bill', 'last_name': 'Clinton', 'gender': 'Male'}, {'name': 'Mumbai', 'population': '19,000,000', 'country': 'India'},
{'first_name': 'Pat', 'last_name': 'Smith', 'gender': 'Unknown'}, {'name': 'New York', 'population': '20,000,000', 'country': 'USA'},
{'first_name': 'Margaret', 'last_name': 'Thatcher', 'gender': 'Female'}, {'name': 'Calcutta', 'population': '15,000,000', 'country': 'India'},
{'first_name': 'George', 'last_name': 'Bush', 'gender': 'Male'}, {'name': 'Chicago', 'population': '7,000,000', 'country': 'USA'},
{'first_name': 'Condoleezza', 'last_name': 'Rice', 'gender': 'Female'}, {'name': 'Tokyo', 'population': '33,000,000', 'country': 'Japan'},
] ]


With this input for ``people``, the example ``{% regroup %}`` template code With this input for ``cities``, the example ``{% regroup %}`` template code
above would result in the following output: above would result in the following output:


* Male: * India

* Mumbai: 19,000,000
* Bill Clinton * USA

* New York: 20,000,000
* Unknown: * India

* Calcutta: 15,000,000
* Pat Smith * Japan

* Tokyo: 33,000,000
* Female:

* Margaret Thatcher

* Male:

* George Bush

* Female:

* Condoleezza Rice


The easiest solution to this gotcha is to make sure in your view code that the The easiest solution to this gotcha is to make sure in your view code that the
data is ordered according to how you want to display it. data is ordered according to how you want to display it.


Another solution is to sort the data in the template using the Another solution is to sort the data in the template using the
:tfilter:`dictsort` filter, if your data is in a list of dictionaries:: :tfilter:`dictsort` filter, if your data is in a list of dictionaries::


{% regroup people|dictsort:"gender" by gender as gender_list %} {% regroup cities|dictsort:"country" by country as country_list %}



Grouping on other properties Grouping on other properties
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^


Any valid template lookup is a legal grouping attribute for the regroup Any valid template lookup is a legal grouping attribute for the regroup
tag, including methods, attributes, dictionary keys and list items. For tag, including methods, attributes, dictionary keys and list items. For
example, if the "gender" field is a foreign key to a class with example, if the "country" field is a foreign key to a class with
an attribute "description," you could use:: an attribute "description," you could use::


{% regroup people by gender.description as gender_list %} {% regroup cities by country.description as country_list %}


Or, if ``gender`` is a field with ``choices``, it will have a Or, if ``country`` is a field with ``choices``, it will have a
:meth:`^django.db.models.Model.get_FOO_display` method available as an :meth:`^django.db.models.Model.get_FOO_display` method available as an
attribute, allowing you to group on the display string rather than the attribute, allowing you to group on the display string rather than the
``choices`` key:: ``choices`` key::


{% regroup people by get_gender_display as gender_list %} {% regroup cities by get_country_display as country_list %}


``{{ gender.grouper }}`` will now display the value fields from the ``{{ country.grouper }}`` will now display the value fields from the
``choices`` set rather than the keys. ``choices`` set rather than the keys.


.. templatetag:: spaceless .. templatetag:: spaceless
Expand Down
19 changes: 10 additions & 9 deletions docs/topics/db/models.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -172,21 +172,22 @@ ones:
from django.db import models from django.db import models


class Person(models.Model): class Person(models.Model):
GENDER_CHOICES = ( SHIRT_SIZES = (
(u'M', u'Male'), (u'S', u'Small'),
(u'F', u'Female'), (u'M', u'Medium'),
(u'L', u'Large'),
) )
name = models.CharField(max_length=60) name = models.CharField(max_length=60)
gender = models.CharField(max_length=2, choices=GENDER_CHOICES) shirt_size = models.CharField(max_length=2, choices=SHIRT_SIZES)


:: ::


>>> p = Person(name="Fred Flintstone", gender="M") >>> p = Person(name="Fred Flintstone", shirt_size="L")
>>> p.save() >>> p.save()
>>> p.gender >>> p.shirt_size
u'M' u'L'
>>> p.get_gender_display() >>> p.get_shirt_size_display()
u'Male' u'Large'


:attr:`~Field.default` :attr:`~Field.default`
The default value for the field. This can be a value or a callable The default value for the field. This can be a value or a callable
Expand Down

0 comments on commit 45d4331

Please sign in to comment.