2012-07-05 137 views
4

我想让Django模型有一个日期字段,其中年,月和日都是可选的。示例值可以与2008一样通用,或者与May 10, 2008一样具体。是否可以定义一个DateField以表现这种行为?或者我最好将年/月/日定义为单独的整数,像这样?Django模型日期字段中的可选年/月/日

model Book(models.Model): 
    publication_year = models.IntegerField(blank=True, null=True) 
    publication_month = models.IntegerField(blank=True, null=True) 
    publication_day = models.IntegerField(blank=True, null=True) 
+0

“5月10日”是您的需求的有效日期吗? – cha0site 2012-07-05 23:01:23

+0

是 - 每个值(年,月,日)都是可选的。 – 2012-07-06 00:45:05

回答

2

Django的类DateTimeField字段是基于Python的datetime.datetime所在班级的年,月和日是必需的:

datetime.datetime(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]]) 

在你的地方,而不是定义三个整场我会使用Django的DateTimeField和和一个布尔值,如果我只应该采取一年或整个日期的话,我就会感到困惑。