【发布时间】:2021-09-24 15:07:50
【问题描述】:
我将JSONField 用于JSON 类型的Django 模型,但在后台,它使我的属性列类型为JSONB,我不想在这里这样做,因为它破坏了我对JSON 内部字段的确切顺序我从我的前端应用程序推送。您可以看到字段的顺序不是我的确切值。要按原样存储,我需要使用 JSON 而不是 jsonb。所以我的问题是如何做到这一点?
我推送的内容:
{
"type": "array",
"title": "action_right",
"additionalProperties": true,
"items": {
"type": "object",
"required": [
"label",
"url"
],
"properties": {
"label": {
"type": "string",
"custom_type": "string",
"title": "label",
"default": ""
},
"url": {
"type": "string",
"custom_type": "string",
"title": "url",
"default": ""
}
}
}
}
存储的内容:
{
"type": "array",
"items": {
"type": "object",
"required": [
"label",
"url"
],
"properties": {
"url": {
"type": "string",
"title": "url",
"default": "",
"custom_type": "string"
},
"label": {
"type": "string",
"title": "label",
"default": "",
"custom_type": "string"
}
}
},
"title": "action_right",
"additionalProperties": true
}
代码 sn-ps:
class Component(models.Model):
name = models.CharField(max_length=200, unique=True)
attributes = models.JSONField(default=dict, help_text='Component attributes in JSONSchema')
注意事项:
PostgreSQL 有两种基于 JSON 的原生数据类型:json 和 jsonb。这 它们之间的主要区别在于它们的存储方式和存储方式 被查询。 PostgreSQL 的 json 字段存储为原始字符串 JSON 的表示形式,必须在查询时动态解码 基于键。 jsonb字段根据实际结构存储 允许索引的 JSON。权衡是一个小的额外 写入 jsonb 字段的成本。 JSONField 使用 jsonb。
【问题讨论】:
标签: json django postgresql jsonb