Search code examples
djangodatedjango-formsdjango-widget

How to set MaxDate and MinDate of a SelectDateWidget in Django?


How to set MaxDate and MinDate of a SelectDateWidget in Django?

I am using that widget for a birthdate field.

#found in:
from django.forms.extras.widgets import SelectDateWidget
from django import forms

def someform(forms.Form):
    birthdate = forms.DateField(widget=SelectDateWidget())

So how do I change its MaxDate and MinDate? its default MinDate is this year and its MaxDate is 9 years after this year.

I want the MinDate to be maybe 100 years before this year and the Maxdate is 10 years before this year.

How do I do that?


Solution

  • You use the years= argument:

    def someform(forms.Form):
        birthdate = forms.DateField(widget=SelectDateWidget(years=range(1990,2011))