[英]Django - in the model, created_at is a UNIX timestamp
I am new to Django. 我是Django的新手。 I would have better grasp of this in Rails but am not familiar enough with all the libraries and converstions in Python / Django.
我会在Rails中更好地掌握这一点,但对Python / Django中的所有库和会话还不够熟悉。
I have a model: 我有一个模型:
class Status(models.Model):
created = models.DatetimeField(auto_now_add=True)
This results in the format: 结果为:
"created": "2017-01-06T22:21:51.531723Z"
I was hoping to see if before saving to the DB that it stores as a UNIX value? 我希望在将其存储为UNIX值存储到数据库之前查看是否?
This looked promising https://stackoverflow.com/a/34843855/3007294 but can't get it working. 这看起来很有希望https://stackoverflow.com/a/34843855/3007294,但无法正常工作。 Every time I try to create a Status object, I get this error:
每次尝试创建Status对象时,都会出现此错误:
Datetime has wrong format. Use one of these formats instead: YYYY-MM-DDThh:mm[:ss[.uuuuuu]][+HH:MM|-HH:MM|Z].
My intentions are to not have to enter this every time a Status object is created. 我的意图是不必在每次创建Status对象时都输入此内容。
Thanks - 谢谢 -
My intentions are to not have to enter this every time a Status object is created.
我的意图是不必在每次创建Status对象时都输入此内容。
This is already taken care of with the auto_now_add=True
option . auto_now_add=True
选项已经解决了auto_now_add=True
。 When you create a Status
object, the created
field will be populated with the current datetime. 创建
Status
对象时,将使用当前日期时间填充created
字段。
I was hoping to see if before saving to the DB that it stores as a UNIX value?
我希望在将其存储为UNIX值存储到数据库之前查看是否?
You can convert the datetime
value from Status.created
to a Unix timestamp anytime required. 您可以随时将
datetime
值从Status.created
转换为Unix时间戳。 Storing it in the default database datetime format (which is what DatetimeField
does) is generally preferred to storing it as a timestamp, which is just an integer. 通常,以默认数据库日期时间格式(即
DatetimeField
所做的)存储它比将其存储为时间戳(只是整数)更可取。
However, the library used in the example you provided, django-unixdatetimefield
, states that "UnixDateTimeField is based on the implementation of the standard Django DateTimeField, making it 100% compatible with all features and options it supports." 但是,在您提供的示例中使用的库
django-unixdatetimefield
指出:“ UnixDateTimeField基于标准Django DateTimeField的实现,使其与所支持的所有功能和选项100%兼容。” So, you should be able to provide the auto_now_add=True
option to that field. 因此,您应该能够为该字段提供
auto_now_add=True
选项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.