Hello: The question that arises is how I can load the options of a form dynamically.
I have this form:
MESES_CHOICE = (
('','Selecciona'),
('01','Enero'),
('02','Febrero'),
('03','Marzo'),
('04','Abril'),
('05','Mayo'),
('06','Junio'),
('07','Julio'),
('08','Agosto'),
('09','Septiembre'),
('10','Octubre'),
('11','Noviembre'),
('12','Diciembre'),)
AÑO_CHOICE = (
('','Selecciona'),
('2018','2018'),
('2017','2017'),)
class FechaForm(forms.Form):
def __init__(self, *args, **kwargs):
super(self.__class__, self).__init__(*args, **kwargs)
self.fields['mes'].required = False
self.fields['año'].required = False
mes = forms.ChoiceField(choices=MESES_CHOICE)
año = forms.ChoiceField(choices=AÑO_CHOICE)
I need to replace the year choice with a dynamic list that is generated from the current year to 2017 so I don't have to modify it by hand every year
You can calculate it using for example the time module and creating a function to calculate from an initial year to the current year. In the case that I put it is from 2010
And change the line where you define the year by