最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - A calender for Django DateField that only shows months and years? - Stack Overflow

programmeradmin3浏览0评论

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 badges
Add a ment  | 

1 Answer 1

Reset to default 7

The 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.

发布评论

评论列表(0)

  1. 暂无评论