简体   繁体   English

使用字符串中的日期时间创建Django模型

[英]Creating a Django model with datetimes from strings

Given the following Django model: 给定以下Django模型:

from django.db import models

class TimeStamp(models.Model):
    date = models.DateField()

I was surprised to discover that I can pass in strings instead of datetime objects: 我惊讶地发现我可以传入字符串而不是datetime对象:

In [4]: ts = TimeStamp.objects.create(date="2001-12-31")

In [5]: ts.date
Out[5]: '2001-12-31'

In [6]: reloaded_ts = TimeStamp.objects.get(id=ts.id)

In [7]: reloaded_ts.date
Out[7]: datetime.date(2001, 12, 31)

Why does Django do this instead of erroring about types? 为什么Django这样做而不是错误的类型? Is there documentation for this behaviour? 是否有有关此行为的文档? What time format is it expecting? 期望什么时间格式?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM