【问题标题】:Decimal('12000') is not JSON serializableDecimal('12000') 不是 JSON 可序列化的
【发布时间】:2014-06-14 03:50:24
【问题描述】:

我有一个在 Apache2 上运行的 Django 实例。我的一个网页正在对 Django 进行 Ajax 回调。

大多数时候,它工作得很好,但有时由于我无法确定的原因,它停止工作并出现来自 Django 的以下错误:

Decimal('12000') 不是 JSON 可序列化的

一旦我收到此错误,它就会永远停止工作。我知道的唯一解决方案是重新启动 Apache。

Python/Django 没有改变,Ajax 调用请求的数据也没有改变。当我在 Django Web 服务器上运行我的代码时,我从来没有遇到过这个问题。

views.py:

@login_required
def KPIInitTable(request):
    kpi_values = [ob.as_json() for ob in KPI.objects.all()]
    data = {'table_data' : kpi_values,
    'colHeaders': ['KPI', 'CityCategory', 'Type','January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
    'rowHeaders': 0,
    'KPIList': settings.TST_KPILIST,
    'TypeList': settings.TST_TYPELIST,
    'CityCategoryList': settings.TST_CITYCATEGORYLIST
    }
    response = HttpResponse(dumps(data, cls=DjangoJSONEncoder), mimetype='application/json')
    response['Cache-Control'] = 'no-cache'
    return response

模型.py:

from django.db import models

    class KPI(models.Model):
        name = models.CharField(max_length=50, null=True)
        CityCategory = models.CharField(max_length=50,  null=True)
        type = models.CharField(max_length=20, null=True)   
        Jan = models.DecimalField( max_digits=15, decimal_places=2, null=True)
        Feb = models.DecimalField( max_digits=15, decimal_places=2, null=True)
        Mar = models.DecimalField( max_digits=15, decimal_places=2, null=True)
        Apr = models.DecimalField( max_digits=15, decimal_places=2, null=True)
        May = models.DecimalField( max_digits=15, decimal_places=2, null=True)
        Jun = models.DecimalField( max_digits=15, decimal_places=2, null=True)
        Jul = models.DecimalField( max_digits=15, decimal_places=2, null=True)
        Aug = models.DecimalField( max_digits=15, decimal_places=2, null=True)
        Sep = models.DecimalField( max_digits=15, decimal_places=2, null=True)
        Oct = models.DecimalField( max_digits=15, decimal_places=2, null=True)
        Nov = models.DecimalField( max_digits=15, decimal_places=2, null=True)
        Dec = models.DecimalField( max_digits=15, decimal_places=2, null=True)
        uniqueFields = ['name','CityCategory','type']
        class Meta:
            permissions = (
                ("access_tst", "Access target setting tool."),
                ("run_tst", "Compute new targets."),
                ("validate_tst", "Validate and load new targets."),
        )
        def as_json(self):
            return {0: self.name, 1: self.CityCategory, 2: self.type, 3: self.Jan,4: self.Feb,5: self.Mar,6: self.Apr,7: self.May,8: self.Jun,9: self.Jul,10: self.Aug,11: self.Sep,12: self.Oct,13: self.Nov,14: self.Dec}

这个值“12000”确实是数据库(SQLite)返回的第一行的第一个十进制值。

有什么想法吗?

谢谢,

【问题讨论】:

  • 什么是as_json() 方法?请显示定义。
  • 模型中的as_json()方法定义如下:def as_json(self): return {0: self.name, 1: self.CityCategory, 2: self.type, 3: self.Jan,4: self.Feb,5: self.Mar,6: self.Apr,7: self.May,8: self.Jun,9: self.Jul,10: self.Aug,11: self.Sep,12: self.Oct,13: self.Nov,14: self.Dec}
  • 你使用的是什么 django 版本?
  • Python 2.7 / Django 1.5.5 / Apache 2 / mod_wsgi.so

标签: ajax json django serialization


【解决方案1】:

解决问题的一种方法是更改​​to_json() 方法并将所有十进制字段值转换为str

def as_json(self):             
    return {0: self.name, 
            1: self.CityCategory, 
            2: self.type, 
            3: str(self.Jan),
            4: str(self.Feb), 
            ...
            14: str(self.Dec)}

【讨论】:

  • 好的,我已经以这种方式更新了我的代码。如果有效,我会尽快通知您!非常感谢。
  • @pfc 是的,这基本上是一种解决方法。我仍然想知道为什么DjangoJSONEncoder 不会序列化十进制字段 - 它knows 怎么做。
  • 这是一种解决方法,但似乎是一种 hack。为什么我们必须将小数转换为字符串?这在技术上似乎不正确,因为正确的 JSON 表示应该只是数字?例如f = Decimal('2.0010') -> {"f": 2.0010} 而不是 {"f": "2.0010"}
猜你喜欢
  • 2018-11-15
  • 2013-04-26
  • 1970-01-01
  • 2010-12-29
  • 2015-01-07
  • 2019-07-05
相关资源
最近更新 更多