DECIMAL类型字段获取数据变为字符串 [英] The DECIMAL type field fetch data become string

查看:20
本文介绍了DECIMAL类型字段获取数据变为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的表格中:

我的折扣类型是小数:

我在表中的数据:

但是为什么当我在API中获取数据时,会出现字符串?

我使用Django和Django睡觉框架作为后台。


belong_product: "实体服务器"
ctime: "2018-04-11T15:41:15.744959+08:00"
desc: ""
discount: "0.005"
id: 1
is_enable: false
max_count: 5
min_count: 0
name: "基数折扣第一阶"
uptime: "2018-04-11T15:41:15.745226+08:00"

我的ListAPI视图:

class DiscountItemByUserOwnCountListAPIView(ListAPIView):
    serializer_class = DiscountItemByUserOwnCountSerializer
    permission_classes = [IsSuperAdmin]
    pagination_class = CommonPagination
    def get_queryset(self):
        return DiscountItemByUserOwnCount.objects.all()

我的型号:

class DiscountItemByUserOwnCount(models.Model):
    name = models.CharField(max_length=16, help_text="名称")
    desc = models.CharField(max_length=512, null=True, blank=True, help_text="描述")
    min_count = models.IntegerField(help_text="范围的最小数目")
    max_count = models.IntegerField(help_text="范围的最大数目")  # 最小~最大 组成范围
    discount = models.DecimalField(max_digits=4, decimal_places=3, default=0.000, unique=True,
                                   help_text="折点")  # 折点: 0.001
    belong_product = models.CharField(max_length=16, help_text="所属产品(DISCOUNT_PRODUCT_TYPE中去选择)")

    is_enable = models.BooleanField(default=False, help_text="是否启用")

    ctime = models.DateTimeField(auto_now_add=True)
    uptime = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.name
    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ['min_count', '-id']

推荐答案

Django睡觉框架中默认的小数表示为字符串。要禁用此行为,请将以下内容添加到您的settings.py文件中:

REST_FRAMEWORK = {
    'COERCE_DECIMAL_TO_STRING': False,
    # Your other settings
    ...
}

请参阅documentation中的详细信息。

这篇关于DECIMAL类型字段获取数据变为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆