python - 如何使用 Django 将存储为 JSON 的十进制数字转换为十进制?
问题描述
我使用 Django 在 JSONField 中存储十进制数字,以便拥有灵活的数字数量,但后来我无法使用它们。这就是我的模型的样子。
from decimal import Decimal
# from django.core.serializers.json import DjangoJSONEncoder
import simplejson
class Order(models.Model):
account = models.ForeignKey(Account, on_delete=models.CASCADE, null=True)
size = JSONField(null=True)
按照此答案https://stackoverflow.com/a/3148376/3423825的建议并存储如下值:
import simplejson as json
self.size = json.dumps(Decimal('1'), use_decimal=True)
现在,当我尝试将其转换为十进制时,出现错误。
size = Decimal(self.size)
Object of type 'Decimal' is not JSON serializable
我必须说我有点迷路,因为type(self.size)
返回<class string>
所以我应该能够将它转换为Decimal
,或者我错过了什么?
谢谢
解决方案
推荐阅读
- javascript - problem regarding copying/taking a node from the binary tree
- .net - 如何在 Blazor 中生成 deps.json 和 runtimeconfig.json 文件?
- python - 由于 InvalidSchema 错误,将 Pandas 上传到 BigQuery 失败
- docker - 在 AWS 上的 docker 容器中在 jenkins 上测试基于 tkinter 的功能
- python - 如何缩小绘图中的 XGB 子树并保存为图像?
- database - 从多个微服务与同一数据库表交互的最佳方法
- javascript - AnguluarJS 阻止缓存一个特定的模板加载
- flutter - 从屏幕上的链接导航时更改底部导航焦点
- python - 为什么过滤的日期范围与我的开始日期不同?
- css - 如何用css制作像诺基亚N9这样的图标?