其他分享
首页 > 其他分享> > django to_representation 日期字符串转时间戳返回

django to_representation 日期字符串转时间戳返回

作者:互联网

代码:

class ProjectSerializer(serializers.ModelSerializer):
    project_name = serializers.CharField(max_length=50, required=True,
                                         validators=[UniqueValidator(queryset=models.ProjectInformation.objects.all())]
                                         )
    tester_charge = serializers.CharField(max_length=100, required=True)
    product_manager = serializers.CharField(max_length=100, required=True)
    project_description = serializers.CharField(max_length=200, allow_null=True, allow_blank=True)

    class Meta:
        model = models.ProjectInformation
        fields = "__all__"

    def to_representation(self, instance):
        data = super().to_representation(instance)
        create_time, update_time = data.get('create_time'), data.get("update_time")
        create_time_stamp = time.mktime(time.strptime(create_time, "%Y-%m-%d %H:%M:%S"))
        update_time_stamp = time.mktime(time.strptime(update_time, "%Y-%m-%d %H:%M:%S"))
        data.update({"create_time": create_time_stamp, "update_time": update_time_stamp})
        return data

 

 

数据库:

 

 

 

返回效果:

 

标签:serializers,data,create,update,django,time,字符串,representation,True
来源: https://www.cnblogs.com/SunshineKimi/p/14850530.html