加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 设置django rest框架配置的DatetimeField格式

发布时间:2020-12-16 23:09:59 所属栏目:Python 来源:网络整理
导读:我尝试使用DRF序列化程序来序列化模型对象.我发现对象中的DatetimeField不会输出ISO-8601格式的“2015-10-21T09:28:53.769000Z” 我查找DRF文档为什么我无法输出ISO-8601格式.根据datetimefield说: format – A string representing the output format. I
我尝试使用DRF序列化程序来序列化模型对象.我发现对象中的DatetimeField不会输出ISO-8601格式的“2015-10-21T09:28:53.769000Z”

我查找DRF文档为什么我无法输出ISO-8601格式.根据datetimefield说:

format – A string representing the output format. If not specified,this defaults to the same value as the DATETIME_FORMAT settings key,which will be ‘iso-8601’ unless set. Setting to a format string indicates that to_representation return values should be coerced to string output. Format strings are described below. Setting this value to None indicates that Python

这意味着如果我从未设置DATETIME_FORMAT参数,则默认输出iso-8601格式?还没有,它仍然没有变化.

当我尝试编写django项目的设置如下:

REST_FRAMEWORK = {
    'DATETIME_FORMAT': "iso-8601",}

或者我在DatetimeField参数中写入如下:

class UserSerializer(...):
    last_login = DatetimeField(format='iso-8601')
    class Meta:
        model = User
        fields = ('email','displayname','is_active','date_joined','last_login')

它仍然没有变化.

有谁知道怎么设置它?

解决方法

如果你不知道发生了什么,你仍然没有解决这个问题,我可以在设置中定义一个datettime格式如下:
REST_FRAMEWORK = {
    'DATETIME_FORMAT': "%Y-%m-%dT%H:%M:%S.%fZ",}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读