choices
, in which the form will display <select> on the page. choices accepts an Iterator (for example, a list or tuple) of 2-element tuples, determining the value options for the field. Example from Django documentation:YEAR_IN_SCHOOL_CHOICES = (
('FR', 'Freshman'),
('SO', 'Sophomore'),
('JR', 'Junior'),
('SR', 'Senior'),
('GR', 'Graduate'),
)
Question actually in the following how to create this iterator in a function if you need to return this iterator:YEARS_BIRTHS = (
('1950', '1950'),
('1951', '1951'),
('1952', '1952'),
...
) Understand that this is the basic thing in Python, but not as you think.</select>
>>> def gen(start, end):
... return ((str(i) str(i)) for i in range(start, end + 1))
...
>>> tuple(gen(1950, 1952))
(('1950', '1950'), ('1951', '1951'), ('1952', '1952'))
>>>
def year_func(*args):
res = ()
for year in range(args[0], args[1]):
res += ((str(year), str(year)),)
return res
class ModelName(models.Model):
year = models.CharField(max_length=50, choices=year_func(1990, 2000), verbose_name='year')
Find more questions by tags Web DevelopmentPythonDjango
Returns
- elenora_Carroll commented on July 9th 19 at 14:02
And it turns out (('1990', '1996'), ('1990', '1990'), ('1991', '1991'), ('1992', '1992'), ('1993', '1993'), ('1994', '1994'), ('1995', '1995')) - elenora_Carroll commented on July 9th 19 at 14:11
- elenora_Carroll commented on July 9th 19 at 14:20
don't want to do that? Malolo will need the same tuple but for other years - Karine.Greenfelder commented on July 9th 19 at 14:26