Reputation: 4924
I am using SelectDateWidget and want to pass in a date range based on max and min date values from my database.
oldest = Item.objects.all().aggregate(Min('date'))['date__min']
start_year = Item.objects.get(date=oldest).date.year # 2012
newest = Item.objects.all().aggregate(Max('date'))['date__max']
end_year = Item.objects.get(date=newest).date.year # 2013
class MyForm(forms.Form):
date_range = forms.DateField(widget=SelectDateWidget(years=range(start_year, end_year)))
The above shows the start_year
only. This is the only choice to select.
This one below works well, and them both seem about the same to me.
date_range = forms.DateField(widget=SelectDateWidget(years=range(2008, 2013)))
Any suggestions please?
Upvotes: 1
Views: 1060
Reputation: 22449
Your perception of the range function is off (the last argument of the range is not included in the list itself, its the limiter)
>> range(2012, 2013)
.. [2012]
You require an offset if you want to include 2013, e.g.
>> range(2012, 2014)
.. [2012, 2013]
Upvotes: 2