In the django admin panel I currently have a DateField showing a nice Javascript calendar for inputting dates. However, I am only interested in the Month and Year. So I was wondering if it was possible to have the calendar only show the months and year. (Extra brownie points if you can restrict its usage so that no dates in the future can be entered.)
A similar question was asked, but not answered, here.
In the django admin panel I currently have a DateField showing a nice Javascript calendar for inputting dates. However, I am only interested in the Month and Year. So I was wondering if it was possible to have the calendar only show the months and year. (Extra brownie points if you can restrict its usage so that no dates in the future can be entered.)
A similar question was asked, but not answered, here.
Share Improve this question edited May 23, 2017 at 12:21 CommunityBot 11 silver badge asked Feb 1, 2010 at 17:19 BioGeekBioGeek 23k23 gold badges90 silver badges154 bronze badges1 Answer
Reset to default 7The easiest (most correct also, since you only care about month and year) thing to do is to break the model field into two fields: month and year. And store that instead.
So instead of having:
class Blah(models.Model):
...
your_date = models.DateField()
You would have:
class Blah(models.Model):
MONTH_CHOICES=((1,"January"), (2,"February"), ...)
...
your_year = models.IntegerField()
your_month = models.IntegerField(choices=MONTH_CHOICES)
And you can implement whatever logic you need to restrict future years or even future months (Year/Month binations) in the clean()
method.
Edit: I say most correct in parenthesis above because you really don't have a date to store. You would have to decide to store the first, or the last, or some arbitrary day of the month/year selected.
Second Edit: As far as javascript for picking a month of a year goes, there is this (see "Month-select calendar" example in that page). It is possible to add javascript to an admin page in Django and use that, but you would - as said in my previous edit - have to choose a day to deal with.