Skip to content

Commit 0632491

Browse files
committed
list vs tuples
1 parent d7c3ddc commit 0632491

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

docs/source/contributing.rst

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,32 @@ Please follow these rules when quoting strings:
116116
* A string intended to be read by humans shall be quoted using double quotes: `"…"`.
117117
* An internal string, such as dictionary keys, etc. (and thus usually not intended to be read by
118118
humans), shall be quoted using single quotes: `'…'`.
119+
120+
121+
Lists versus Tuples
122+
===================
123+
124+
Unfortunately in Django, `we developers far too often`_ intermixed lists and tuples without being
125+
aware of their intention. Therefor please follow this rule:
126+
127+
Always use lists, if there is a theoretical possibility that someday, someone might add an item.
128+
Therefore ``list_display``, ``list_display_links``, ``fields``, etc. shall always be lists.
129+
130+
Always use tuples, if the number of items is restricted by nature, and there isn't even a
131+
theoretical possibility of being extended.
132+
133+
Example:
134+
135+
.. code-block:: python
136+
137+
color = ChoiceField(
138+
label="Color",
139+
choices=[('ff0000', "Red"), ('00ff00', "Green"), ('0000ff', "Blue")],
140+
)
141+
142+
A ``ChoiceField`` must provide a list of choices. Attribute ``choices`` must be a list because
143+
it is eligible for extension. Its inner items however must be tuples, because they can exlusively
144+
containin the choice value and a human readable label. Here we also intermix single with double
145+
quotes to distinguish strings intended to be read by the machine versus a human.
146+
147+
.. _we developers far too often: https://groups.google.com/g/django-developers/c/h4FSYWzMJhs

docs/source/usage.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ through the model's ``Meta`` class. Here's an example ``models.py``:
3434
null=False,
3535
)
3636
37-
class Meta(object):
37+
class Meta:
3838
ordering = ['my_order']
3939
4040
Here the ordering field is named ``my_order``, but any valid Python variable name will work. There

0 commit comments

Comments
 (0)