首页 > 其他分享 >drf框架时间处理问题

drf框架时间处理问题

时间:2022-12-21 13:33:18浏览次数:55  
标签:-% 框架 处理 配置 时间 location 格式 document drf

问题:时间显示多个T和毫秒等,不是中国人常用的时间格式

解决方法:

第一种:全局配置

在settings.py的REST_FRAMEWORK下配置(全局生效)

REST_FRAMEWORK = {
    # 时间格式配置
    'DATETIME_FORMAT': "%Y-%m-%d %H:%M:%S",  # 日期时间格式配置
    'DATE_FORMAT': "%Y-%m-%d",
}

第二种:局部配置

在序列化器中添加时间字段并赋予格式

class ProjectSerializer(serializers.ModelSerializer):
    # 指定时间格式
    create_datetime = serializers.DateTimeField('%Y年%m')

    class Meta:
        model = Project
        fields = '__all__'

 

TRANSLATE with x English
Arabic Hebrew Polish
Bulgarian Hindi Portuguese
Catalan Hmong Daw Romanian
Chinese Simplified Hungarian Russian
Chinese Traditional Indonesian Slovak
Czech Italian Slovenian
Danish Japanese Spanish
Dutch Klingon Swedish
English Korean Thai
Estonian Latvian Turkish
Finnish Lithuanian Ukrainian
French Malay Urdu
German Maltese Vietnamese
Greek Norwegian Welsh
Haitian Creole Persian  
  TRANSLATE with COPY THE URL BELOW Back EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: Bing Webmaster Portal Back

标签:-%,框架,处理,配置,时间,location,格式,document,drf
From: https://www.cnblogs.com/u-damowang1/p/16996036.html

相关文章