rest_framework 2.4.x和3.1.x中的DecimalField

时间:2015-04-29 12:32:37

标签: python django django-rest-framework

我读到here,2.1.16版本中DecimalField的表示形式已从float更改为string。

我当前的版本是 2.4.x ,而DecimalField表示为一个对我有用的字符串。但是现在测试最新版本( 3.1.1 ),DecimalField表示为float而不是string。

根据this默认情况下,它应该是字符串而不是浮点数。

即使这样设置:

REST_FRAMEWORK['COERCE_DECIMAL_TO_STRING'] = True / False

我还有漂浮物。是否有任何(其他)方法强制DecimalFields的字符串输出?

我'需要一些全球解决方案。

感谢。

- - - - - - - - 编辑

class FakeView(APIView):
    def get(self, request):
        balance = {'id': 5, 'name': 'apple', 'value': Decimal(2.5)}
        return Response(data = balance)

发生在v2.4.3:

{
    id: 5,
    value: "2.5",
    name: "apple"
}

V3.1.1:

{
    id: 5,
    value: 2.5,
    name: "apple"
}

所以区别在于值的类型,而且我是' d喜欢在v3.1.1中使用str类型。

0 个答案:

没有答案