【问题标题】:Django defining a json type on model makes jsonb instead of jsonDjango 在模型上定义 json 类型使 jsonb 代替 json
【发布时间】: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


    【解决方案1】:

    试试这个(可能需要调整):

    import json
    
    class RawJSONField(models.JSONField):
        def db_type(self, connection):
            return 'text'
    
        def get_prep_value(self, value):
            return json.dumps(value)
    
        def from_db_value(self, value, expression, connection):
            if value is None:
                return value
            return json.loads(value)
    

    在某些模型中:

    class Calculation(models.Model):
        calc_result = RawJSONField(_("Calc result data"), default = dict)
    

    其他变体:

    from django.db import models
    import json
    
    class RawJSONField(models.TextField):
        def get_prep_value(self, value):
            return json.dumps(value)
    
        def from_db_value(self, value, expression, connection):
            if value is None:
                return value
            return json.loads(value)
    

    【讨论】:

      猜你喜欢
      • 2014-11-11
      • 2016-02-29
      • 2017-04-14
      • 2019-02-17
      • 2014-10-05
      • 2021-11-15
      • 2021-11-30
      • 2019-08-12
      相关资源
      最近更新 更多