Year dropdown in Django admin

5,473 views
Skip to first unread message

grimmus

unread,
Feb 20, 2012, 9:47:37 AM2/20/12
to django...@googlegroups.com
Hi,

I have a car model that contains many fields including a 'year' field. I need the dropdown for this field to display the current year as the first option and also display the previous 25 years as individual options.

I was thinking i could create a list object and then populate the list based on the current year and work my way back to 25 years earlier.

I am not sure how to implement this so it would work in the Django admin area.

Could someone please point me in the right direction with this ?

Thank you in advance.


Denis Darii

unread,
Feb 20, 2012, 9:55:43 AM2/20/12
to django...@googlegroups.com
I was in the same situation as you and I found this solution...
in your models.py:
import datetime
YEAR_CHOICES = [] for r in range(1980, (datetime.datetime.now().year+1)): YEAR_CHOICES.append((r,r))

so, your field can now use YEAR_CHOICES:
year = models.IntegerField(_('year'), max_length=4, choices=YEAR_CHOICES, default=datetime.datetime.now().year)




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/NUO22GxW7UEJ.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.



--
This e-mail and any file transmitted with it is intended only for the person or entity to which is addressed and may contain information that is privileged, confidential or otherwise protected from disclosure. Copying, dissemination or use of this e-mail or the information herein by anyone other than the intended recipient is prohibited. If you are not the intended recipient, please notify the sender immediately by return e-mail, delete this communication and destroy all copies.

grimmus

unread,
Feb 20, 2012, 10:03:41 AM2/20/12
to django...@googlegroups.com
Excellent,

Thanks very much
Reply all
Reply to author
Forward
0 new messages