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

 

 

数据库:

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

 

 

 

返回效果:

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

 

上一篇:Oracle Data Guard_ 主库添加数据文件或创建表空间


下一篇:django中的to_internal_value和to_representation