admin管理员组文章数量:1390939
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.
本文标签: javascriptA calender for Django DateField that only shows months and yearsStack Overflow
版权声明:本文标题:javascript - A calender for Django DateField that only shows months and years? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744608878a2615527.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论