DjangoRestFramework ModelSerializer DateTimeField仅在创建对象时转换为当前时区

标签 django datetime serialization django-rest-framework

更新:django-rest-framework 3.7中将此行为识别为错误并已解决。现在,序列化程序将根据服务器的时区一致地显示时间:https://github.com/tomchristie/django-rest-framework/issues/3732#issuecomment-267635612

我有一个Django项目,我希望用户位于某个时区。我的设置中有TIME_ZONE = 'Asia/Kolkata'USE_TZ = True

我有一个包含datetimefield的模型。当我第一次创建对象时,Modelerializer会给日期时间加上尾随的+5:30。令人讨厌的是,带有auto_now_add=True的datetimes给UTC日期时间带有尾随的Z。我通过将字段的默认值设置为当前时间的可调用项来解决此问题。

如果我在任何时候再次序列化该对象,则所有日期时间都以UTC开头,并带有尾随Z。从Django文档中,我希望序列化程序使用当前时区,该时区默认为TIME_ZONE = 'Asia/Kolkata'设置的默认时区。我已经使用get_current_timezone_name()在我的 View 中检查了当前时区,它是'Asia/Kolkata'。我什至尝试过在自己的 View 中使用activate('Asia/Kolkata'),但时间仍以UTC返回。

请注意,所有时间都是正确的(UTC时间早5:30小时),这正是我希望转换的时间。所有日期时间都按照预期的UTC时间存储在数据库中。

最佳答案

在这里查看文档:http://www.django-rest-framework.org/api-guide/fields/#datetimefield

Signature: DateTimeField(format=None, input_formats=None)

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 objects should be returned by to_representation. In this case the datetime encoding will be determined by the renderer.

When a value of None is used for the format datetime objects will be returned by to_representation and the final output representation will determined by the renderer class.

In the case of JSON this means the default datetime representation uses the ECMA 262 date time string specification. This is a subset of ISO 8601 which uses millisecond precision, and includes the 'Z' suffix for the UTC timezone, for example: 2013-01-29T12:34:56.123Z.



因此,获取日期时间对象的UTC(Z)表示实际上是默认行为。

当您通过Django Django Rest创建或创建更新模型实例时,将使用data kwarg调用序列化程序,如果您使用 ListView 或详细信息 View ,则不会发生。

在这两种情况下,您的 View 都将返回serializer.data。在创建/更新操作的情况下,这将是serializer.validated_data的表示形式,而在列表/详细信息操作的情况下,它将是实例的直接表示形式。

在这两种情况下,都可以通过使用默认的kwarg field.to_representation调用format=None来实现表示,这将使该字段返回纯字符串值。

魔术发生在here上:
  • 创建/更新:验证返回一个时区感知对象,其中包括您的标准时区。通过调用其isoformat()方法将其转换为字符串,并按原样返回。
  • 列表/检索:django ORM将时间戳存储为UTC。通过调用其isoformat()方法将其转换为字符串,但DRF将+00:00替换为Z (请参见上面的链接)。

  • 因此,要获得具有时区偏移量的所需输出,可以将format=None或自定义strftime字符串传递给序列化器中的DateTimeField。但是,您将始终使用+00:00来获取UTC时间,因为(幸运地)这就是存储数据的时间。

    如果您想要'Asia/Kolkata'的实际偏移量,则可能必须定义自己的DateTimeField:
    from django.utils import timezone
    class CustomDateTimeField(serializers.DateTimeField):
        def to_representation(self, value):
            tz = timezone.get_default_timezone()
            # timezone.localtime() defaults to the current tz, you only
            # need the `tz` arg if the current tz != default tz
            value = timezone.localtime(value, timezone=tz)
            # py3 notation below, for py2 do:
            # return super(CustomDateTimeField, self).to_representation(value)
            return super().to_representation(value) 
    

    关于DjangoRestFramework ModelSerializer DateTimeField仅在创建对象时转换为当前时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34275588/

    相关文章:

    django - 如何在Django中的 'python manage.py runserver'之后继续在终端中输入?

    PHP:在日期时间之间搜索 MYSQL 数据库但仅使用日期?

    datetime - 加载日期时间字段在 pig latin 0.12 中不起作用

    javascript - Js Date 对象到 python datetime

    javascript - 使用 json.net 序列化为对象

    python - Django:新版本Django中的DisallowedHost

    python - 在 Django 中获取按月分组的结果

    python - auth.User.groups : (fields. E304) 'User.groups' 的反向访问器与 'UserManage.groups' 的反向访问器冲突

    c# - 使用 BinaryFormatter 序列化和反序列化对象图

    c# - 系统.Runtime.Serialization.SerializationException : Unable to find assembly MyAssembly