DateTimeField成为datetime.datetime
Python中的对象
如果
date以后需要使用某个对象进行操作,则可以使用以下方法
datetime.date直接从中拉出该对象:
DateTimeField()
datetime.datetime.date()
class ApplicantData(models.Model): scheduled_at = models.DateTimeField(null=True, blank=True)date = application_data.scheduled_at.date()
之所以有效,是因为Django会将转换
DateTimeField为
datetime.datetime我们调用的Python类型
date()。
格式化为datetime.date
您想要的
然后,您将获得一个
datetime.date对象,可以使用随意设置其格式
datetime.date.strftime()。
如果不需要
date对象,那么也可以
strftime在
datetime.datetime对象上使用它,这没有问题。除了您的对象中有一个None字段。
处理NULL
/None
领域
如果要允许使用NULL值,则
scheduled_at可以执行以下操作:
if application_data.scheduled_at is not None: date = application_data.scheduled_at.date()



