Django

Code

Changeset 3094

Show
Ignore:
Timestamp:
06/06/06 21:46:08 (2 years ago)
Author:
adrian
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/core/management.py

    r3083 r3094  
    837837                e.add(opts, '"%s": prepopulate_from should be a list or tuple.' % f.name) 
    838838            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) 
    841841                else: 
    842842                    for c in f.choices: 
  • django/trunk/docs/model-api.txt

    r3059 r3094  
    446446~~~~~~~~~~~ 
    447447 
    448 A list of 2-tuples to use as choices for this field. 
     448An iterable (e.g., a list or tuple) of 2-tuples to use as choices for this 
     449field. 
    449450 
    450451If this is given, Django's admin will use a select box instead of the 
     
    481482    class Foo(models.Model): 
    482483        gender = models.CharField(maxlength=1, choices=GENDER_CHOICES) 
     484 
     485Finally, note that choices can be any iterable object -- not necessarily a 
     486list or tuple. This lets you construct choices dynamically. But if you find 
     487yourself hacking ``choices`` to be dynamic, you're probably better off using 
     488a proper database table with a ``ForeignKey``. ``choices`` is meant for static 
     489data that doesn't change much, if ever. 
    483490 
    484491``core``