【问题标题】:django datefime filed ignore milisecond and extra datadjango datetime 字段忽略毫秒和额外数据
【发布时间】:2018-07-27 05:41:09
【问题描述】:

默认情况下,Django DateTime 在数据中保存并显示秒和毫秒以及额外信息,例如它保存并显示日期如下:

2018-07-21 05:27:29.736956+00:00

在尝试检索数据时,是否有任何方法或任何全局函数来忽略额外信息并仅获取日期和时间?喜欢

2018-07-21 05:27:29

型号:

class Enrolled(models.Model):
    user = models.ForeignKey(User, on_delete=models.DO_NOTHING)
    product = models.ForeignKey(Product, on_delete=models.CASCADE, to_field='product_id',
                                related_name='enroll')
    created_date = models.DateTimeField(auto_now_add=True)

序列化器

class EnrolledSerializer(ModelSerializer):
    ptitle = serializers.SerializerMethodField()
    instructor = serializers.SerializerMethodField()
    instructorid = serializers.SerializerMethodField()
    pslug = serializers.SerializerMethodField()

    def get_instructor(self, object):
        return object.product.author.username

    def get_instructorid(self, object):
        return object.product.author.id

    def get_ptitle(self, obj):
        return obj.product.title

    def get_pslug(self, obj):
        return obj.product.slug

    class Meta:
        model = Enrolled
        fields = [
            'id',
            'user',
            'product',
            'ptitle',
            'instructorid',
            'pslug',
            'instructor',
            'created_date',
        ]

        read_only_fields = ['product', 'created_date', 'id', 'user', 'instructor', 'instructorid', 'pslug', 'ptitle']

//编辑:

REST_FRAMEWORK = {
    'DATETIME_FORMAT': ("%Y-%m-%d %H:%M"),
    'DEFAULT_PERMISSION_CLASSES': (
        'rest_framework.permissions.AllowAny',
    ),
    'DEFAULT_AUTHENTICATION_CLASSES': (

        'rest_framework_jwt.authentication.JSONWebTokenAuthentication',
        'rest_framework.authentication.SessionAuthentication',
        'rest_framework.authentication.TokenAuthentication',
    ),
    'DEFAULT_RENDERER_CLASSES': (
        'rest_framework.renderers.JSONRenderer',
        'rest_framework.renderers.BrowsableAPIRenderer',
    )
}

USE_I18N = False

USE_L10N = False

USE_TZ = True

【问题讨论】:

    标签: python django django-rest-framework


    【解决方案1】:

    您可以通过设置所有日期时间字段为created_date 设置日期时间格式:

    REST_FRAMEWORK = {
        'DATETIME_FORMAT': "%Y-%m-%d %H:%M",
    }
    

    仅适用于当前序列化程序,如果您的其他字段只是引用,您可以使用source 选项,例如:

    class EnrolledSerializer(ModelSerializer):
        created_date = serializers.DateTimeField(format="%Y-%m-%d %H:%M")
        ptitle = serializers.CharField(source='product.author.username')
    

    阅读文档:date-and-time-fieldssource

    【讨论】:

    • 我使用了 REST_FRAMEWORK 选项,但它对我不起作用我在帖子中发布设置你能检查它为什么不起作用吗?
    【解决方案2】:

    似乎与Change time format in Django Views相关

    使用

    microsecond=0
    

    或者您可以按照模板过滤器https://docs.djangoproject.com/en/2.0/ref/templates/builtins/#std:templatefilter-date 对视图中的日期时间进行格式化

    {{ value|date:"Y-m-d" }} {{ value|time:"H:i:s" }}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-26
      • 2015-06-22
      • 1970-01-01
      • 2022-10-18
      • 1970-01-01
      相关资源
      最近更新 更多