Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #1697 and #2095 -- Made 'choices' parameter accept any iterable

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3094 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit f3f002b1610ac51044e1e6dff5b7ff8f00b9f4dd 1 parent 448bece
Adrian Holovaty authored
4  django/core/management.py
@@ -836,8 +836,8 @@ def get_validation_errors(outfile, app=None):
836 836
             if f.prepopulate_from is not None and type(f.prepopulate_from) not in (list, tuple):
837 837
                 e.add(opts, '"%s": prepopulate_from should be a list or tuple.' % f.name)
838 838
             if f.choices:
839  
-                if not type(f.choices) in (tuple, list):
840  
-                    e.add(opts, '"%s": "choices" should be either a tuple or list.' % f.name)
  839
+                if not hasattr(f.choices, '__iter__'):
  840
+                    e.add(opts, '"%s": "choices" should be iterable (e.g., a tuple or list).' % f.name)
841 841
                 else:
842 842
                     for c in f.choices:
843 843
                         if not type(c) in (tuple, list) or len(c) != 2:
9  docs/model-api.txt
@@ -445,7 +445,8 @@ empty value. If a field has ``blank=False``, the field will be required.
445 445
 ``choices``
446 446
 ~~~~~~~~~~~
447 447
 
448  
-A list of 2-tuples to use as choices for this field.
  448
+An iterable (e.g., a list or tuple) of 2-tuples to use as choices for this
  449
+field.
449 450
 
450 451
 If this is given, Django's admin will use a select box instead of the
451 452
 standard text field and will limit choices to the choices given.
@@ -481,6 +482,12 @@ or outside your model class altogether::
481 482
     class Foo(models.Model):
482 483
         gender = models.CharField(maxlength=1, choices=GENDER_CHOICES)
483 484
 
  485
+Finally, note that choices can be any iterable object -- not necessarily a
  486
+list or tuple. This lets you construct choices dynamically. But if you find
  487
+yourself hacking ``choices`` to be dynamic, you're probably better off using
  488
+a proper database table with a ``ForeignKey``. ``choices`` is meant for static
  489
+data that doesn't change much, if ever.
  490
+
484 491
 ``core``
485 492
 ~~~~~~~~
486 493
 

0 notes on commit f3f002b

Please sign in to comment.
Something went wrong with that request. Please try again.