Loading django/core/management.py +2 −2 Original line number Diff line number Diff line Loading @@ -836,8 +836,8 @@ def get_validation_errors(outfile, app=None): if f.prepopulate_from is not None and type(f.prepopulate_from) not in (list, tuple): e.add(opts, '"%s": prepopulate_from should be a list or tuple.' % f.name) if f.choices: if not type(f.choices) in (tuple, list): e.add(opts, '"%s": "choices" should be either a tuple or list.' % f.name) if not hasattr(f.choices, '__iter__'): e.add(opts, '"%s": "choices" should be iterable (e.g., a tuple or list).' % f.name) else: for c in f.choices: if not type(c) in (tuple, list) or len(c) != 2: Loading docs/model-api.txt +8 −1 Original line number Diff line number Diff line Loading @@ -445,7 +445,8 @@ empty value. If a field has ``blank=False``, the field will be required. ``choices`` ~~~~~~~~~~~ A list of 2-tuples to use as choices for this field. An iterable (e.g., a list or tuple) of 2-tuples to use as choices for this field. 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. Loading Loading @@ -481,6 +482,12 @@ or outside your model class altogether:: class Foo(models.Model): gender = models.CharField(maxlength=1, choices=GENDER_CHOICES) Finally, note that choices can be any iterable object -- not necessarily a list or tuple. This lets you construct choices dynamically. But if you find yourself hacking ``choices`` to be dynamic, you're probably better off using a proper database table with a ``ForeignKey``. ``choices`` is meant for static data that doesn't change much, if ever. ``core`` ~~~~~~~~ Loading Loading
django/core/management.py +2 −2 Original line number Diff line number Diff line Loading @@ -836,8 +836,8 @@ def get_validation_errors(outfile, app=None): if f.prepopulate_from is not None and type(f.prepopulate_from) not in (list, tuple): e.add(opts, '"%s": prepopulate_from should be a list or tuple.' % f.name) if f.choices: if not type(f.choices) in (tuple, list): e.add(opts, '"%s": "choices" should be either a tuple or list.' % f.name) if not hasattr(f.choices, '__iter__'): e.add(opts, '"%s": "choices" should be iterable (e.g., a tuple or list).' % f.name) else: for c in f.choices: if not type(c) in (tuple, list) or len(c) != 2: Loading
docs/model-api.txt +8 −1 Original line number Diff line number Diff line Loading @@ -445,7 +445,8 @@ empty value. If a field has ``blank=False``, the field will be required. ``choices`` ~~~~~~~~~~~ A list of 2-tuples to use as choices for this field. An iterable (e.g., a list or tuple) of 2-tuples to use as choices for this field. 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. Loading Loading @@ -481,6 +482,12 @@ or outside your model class altogether:: class Foo(models.Model): gender = models.CharField(maxlength=1, choices=GENDER_CHOICES) Finally, note that choices can be any iterable object -- not necessarily a list or tuple. This lets you construct choices dynamically. But if you find yourself hacking ``choices`` to be dynamic, you're probably better off using a proper database table with a ``ForeignKey``. ``choices`` is meant for static data that doesn't change much, if ever. ``core`` ~~~~~~~~ Loading