I want to create a simple html <select>
list.
In models.py
I define a Member
model that has numerous details about each User
. One detail is a year, i.e. 2000, 2001, etc. In a member registration form I want a <select>
list with years to choose from that goes back 20 years (changes dynamically as years go by).
I have learned from this post that I should use ModelChoiceField
with a queryset argument.
It seems that I need to create another model dedicated to the dynamic list but I don't think that is correct because it really doesn't need to be in a database as it can be calculated each time the form is loaded.
So what should I put in the queryset attribute? Ideally, I want to create simple function that obtains the current year creates some type of array that is the queryset. I am not that experienced with python so this may be a dumb question.
RESPONSE TO Daniel Roseman:
Yes, that makes much more sense. Thanks! However, I did have to modify the datetime line to be:
import datetime
year = datetime.date.today().year