无法使用 JSONField 保存到模型
Unable to save to model with JSONField
我被这个问题困扰了很长时间,以至于我什至不确定自己的处境。我编写了以下管理命令,它生成一些 JSON 并使用 Django 模型保存它。后来我有一个视图可以检索此 JSON 并将其显示在 URL.
中
型号如下:
from os import name
from django.db import models
# Create your models here.
class Payload(models.Model):
name = models.CharField(max_length=255)
json = models.JSONField()
管理命令是这样的:
nodes = .... # Various logic to generate a dictionary
links = .... # Various logic to generate a dictionary
payload = json.dumps({'nodes': nodes, 'links': links})
print(payload)
obj = Payload.objects.filter(name='sankey').first()
if obj:
obj.json = payload
result = obj.save()
if result:
print('Successfully updated payload')
else: print('Could not update payload: '+str(result))
else:
obj = Payload(name='sankey', json=payload)
result = obj.save()
if result:
print('Saved new payload')
else: print('Could not create new payload: '+str(result))
self.stdout.write(self.style.SUCCESS('Generatesankeypayload command executed.'))
当我 运行 命令时,我得到:
{"nodes": [{"name": "Jacks Account"}, {"name": "Books"}, {"name": "Coffee & Snacks"}, {"name": "Grooming"}, {"name": "Hobbies"}, {"name": "Personal Gear"}, {"name": "Public Transport"}, {"name": "Jacks Monzo"}, {"name": "Toys"}, {"name": "Janes Account"}, {"name": "Clothing"}, {"name": "Joint Account"}, {"name": "Basic food"}, {"name": "Council tax"}, {"name": "Electricity & Gas"}, {"name": "Mortgage Payments"}], "links": [{"source": 0, "target": 1, "value": "18.98"}, {"source": 0, "target": 2, "value": "7.59"}, {"source": 0, "target": 3, "value": "35.00"}, {"source": 0, "target": 4, "value": "164.98"}, {"source": 0, "target": 5, "value": "300.99"}, {"source": 0, "target": 6, "value": "300.00"}, {"source": 7, "target": 4, "value": "10.99"}, {"source": 7, "target": 8, "value": "22.94"}, {"source": 9, "target": 10, "value": "25.00"}, {"source": 9, "target": 6, "value": "360.00"}, {"source": 11, "target": 12, "value": "641.56"}, {"source": 11, "target": 13, "value": "300.00"}, {"source": 11, "target": 14, "value": "330.00"}, {"source": 11, "target": 15, "value": "3300.00"}]}
Could not update payload: None
Generatesankeypayload command executed.
在玩代码的某个时候,命令确实保存了一些 JSON,但向数据库添加了转义字符。这是 Django 管理面板的屏幕截图:
不要使用 json.dumps()
如果你使用它,它会添加 /
和其他东西。
您可以实现使用 dictionary
JSONField 将像这样处理序列化
payload = {'nodes': nodes, 'links': links}
我被这个问题困扰了很长时间,以至于我什至不确定自己的处境。我编写了以下管理命令,它生成一些 JSON 并使用 Django 模型保存它。后来我有一个视图可以检索此 JSON 并将其显示在 URL.
中型号如下:
from os import name
from django.db import models
# Create your models here.
class Payload(models.Model):
name = models.CharField(max_length=255)
json = models.JSONField()
管理命令是这样的:
nodes = .... # Various logic to generate a dictionary
links = .... # Various logic to generate a dictionary
payload = json.dumps({'nodes': nodes, 'links': links})
print(payload)
obj = Payload.objects.filter(name='sankey').first()
if obj:
obj.json = payload
result = obj.save()
if result:
print('Successfully updated payload')
else: print('Could not update payload: '+str(result))
else:
obj = Payload(name='sankey', json=payload)
result = obj.save()
if result:
print('Saved new payload')
else: print('Could not create new payload: '+str(result))
self.stdout.write(self.style.SUCCESS('Generatesankeypayload command executed.'))
当我 运行 命令时,我得到:
{"nodes": [{"name": "Jacks Account"}, {"name": "Books"}, {"name": "Coffee & Snacks"}, {"name": "Grooming"}, {"name": "Hobbies"}, {"name": "Personal Gear"}, {"name": "Public Transport"}, {"name": "Jacks Monzo"}, {"name": "Toys"}, {"name": "Janes Account"}, {"name": "Clothing"}, {"name": "Joint Account"}, {"name": "Basic food"}, {"name": "Council tax"}, {"name": "Electricity & Gas"}, {"name": "Mortgage Payments"}], "links": [{"source": 0, "target": 1, "value": "18.98"}, {"source": 0, "target": 2, "value": "7.59"}, {"source": 0, "target": 3, "value": "35.00"}, {"source": 0, "target": 4, "value": "164.98"}, {"source": 0, "target": 5, "value": "300.99"}, {"source": 0, "target": 6, "value": "300.00"}, {"source": 7, "target": 4, "value": "10.99"}, {"source": 7, "target": 8, "value": "22.94"}, {"source": 9, "target": 10, "value": "25.00"}, {"source": 9, "target": 6, "value": "360.00"}, {"source": 11, "target": 12, "value": "641.56"}, {"source": 11, "target": 13, "value": "300.00"}, {"source": 11, "target": 14, "value": "330.00"}, {"source": 11, "target": 15, "value": "3300.00"}]}
Could not update payload: None
Generatesankeypayload command executed.
在玩代码的某个时候,命令确实保存了一些 JSON,但向数据库添加了转义字符。这是 Django 管理面板的屏幕截图:
不要使用 json.dumps()
如果你使用它,它会添加 /
和其他东西。
您可以实现使用 dictionary
JSONField 将像这样处理序列化
payload = {'nodes': nodes, 'links': links}