I want my users to enter their birth year. I don't want them to type the same in the form rather select the year from available options. I known that I can do something like this in my model if I needed to date instead of year:
class MyModel(models.Model):
birthday = models.DateField(null=True, blank=True)
I can do this in forms to let the user choose date from datepicker.
birthday = forms.fields.DateField(widget=forms.widgets.DateInput(attrs={'type': 'date'}))
For year, I can use a CharField/IntegerField
with choices
similar to what has been done in this SO answer.
import datetime
YEAR_CHOICES = [(r,r) for r in range(1984, datetime.date.today().year+1)]
year = models.IntegerField(_('year'), choices=YEAR_CHOICES, default=datetime.datetime.now().year)
The problem, however, is that change of current year from say, 2018 to 2019, will not change the available options.
Can you help or provide hints to achieve what I want to do?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…