How to create an iterator two-element tuples?

Are the models in Django, in field settings the argument 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>
July 9th 19 at 13:55
3 answers
July 9th 19 at 13:57
Solution
>>> 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'))
>>>
July 9th 19 at 13:59
Solution
year_min = 1900
year_max = 2000

YEARS_BIRTHS = ((str(year_max), str(year_max)))
for year in range(year_min, year_max):
new_tuple = (str(year), str(year)),
YEARS_BIRTHS += new_tuple

print(YEARS_BIRTHS)
year_min = 1990
year_max = 1996

YEARS_BIRTHS = ((str(year_min), str(year_max)))
for year in range(year_min, year_max):
 new_tuple = (str(year), str(year)),
 YEARS_BIRTHS += new_tuple

print(YEARS_BIRTHS)

Returns
('1990', '1996', ('1990', '1990'), ('1991', '1991'), ('1992', '1992'), ('1993', '1993'), ('1994', '1994'), ('1995', '1995'))
- elenora_Carroll commented on July 9th 19 at 14:02
: YEARS_BIRTHS = ((str(year_min), str(year_max))), <-a comma was forgotten. - Karine.Greenfelder commented on July 9th 19 at 14:05
: blah, and I sit blunt. Thank you, my good man. - elenora_Carroll commented on July 9th 19 at 14:08
damn, still not the same. Should not be the first two values.
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
YEARS_BIRTHS = ((str(year_max), str(year_max))), <- minimum and maximum output, casually written. - Karine.Greenfelder commented on July 9th 19 at 14:14
in range() maximum value is not included and therefore the maximum is added at the beginning when declaring tuple - Karine.Greenfelder commented on July 9th 19 at 14:17
: Something like that
def dob_years():
 year_min = 1950
 year_max = 2006

 YEARS_BIRTHS = ()
 for year in range(year_min, year_max):
 new_tuple = (str(year), str(year)),
 YEARS_BIRTHS += new_tuple
 return YEARS_BIRTHS
class PersonForm(models.Model):
 person_dob_year = models.CharField(
 "Year of birth",
default='1980',
max_length=4,
choices=dob_years(),
blank=True,
null=True,
 )
- elenora_Carroll commented on July 9th 19 at 14:20
I understand that spike places, but just learning. - elenora_Carroll commented on July 9th 19 at 14:23
def dob_years(year_min, year_max):
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
: probably worth it to do so. thanks for the tip. And a Huge Thank you for participating. - elenora_Carroll commented on July 9th 19 at 14:29
July 9th 19 at 14:01
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')


CharField - change what You need.
I know, it's there written. I need this very YEARS_BIRTHS to from a function that will generate the iterator. I just think it is silly to manually write the whole list when it can be generated. - elenora_Carroll commented on July 9th 19 at 14:04
Here the question is no longer Jankowski and Python's. - Karine.Greenfelder commented on July 9th 19 at 14:07
: choices=years_func() , but the function may returns the desired tuple - elenora_Carroll commented on July 9th 19 at 14:10
: Yes, I can't write this function. - elenora_Carroll commented on July 9th 19 at 14:13
from django.utils.functional import lazy
choices=lazy(choices, tuple) - Karine.Greenfelder commented on July 9th 19 at 14:16
:
def year_func(*args):
res = ()
for year in range(args[0], args[1]):
res += ((year, year),)
return res - Karine.Greenfelder commented on July 9th 19 at 14:19

Find more questions by tags Web DevelopmentPythonDjango