datetime.datetime(2017,4,6​​,1,44,44,tzinfo = <utc>)django

时间:2018-01-27 04:15:26

标签: python django datetime django-rest-framework

当我用django-rest-framework创建我的模型序列化程序并尝试从URL获取时我得到了下一个错误

  

'unicode'对象没有属性'tzinfo'

但是当我执行django的shell时

a = Model.objects.all()[0:1]
a[0].upload_date

这是在shell中打印的

  

datetime.datetime(2017,4,6​​,1,44,44,tzinfo =)

模型中的字段

upload_date = models.DateTimeField(blank=True, null=True)

序列化程序

class ReportSerializer(serializers.ModelSerializer):

    class Meta:
        model = Reports
        fields = '__all__'

我不知道如何序列化这个字段

1 个答案:

答案 0 :(得分:0)

class ReportSerializer(serializers.ModelSerializer):
    upload_date = serializers.DateTimeField()
    class Meta:
        model = Reports
        fields = ('upload_date',)

更多信息http://www.django-rest-framework.org/api-guide/fields/#datetimefield