Django JSONField内部的ArrayField [英] Django JSONField inside ArrayField

查看:953
本文介绍了Django JSONField内部的ArrayField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



models.py

  locations = ArrayField(JSONField(null = True,blank = True),blank = True,null = True)

插入

  location_arr = [{locations:loc1,amount:Decimal(100.00)},{locations:loc2,amount:Decimal(200.25)}] 
instance.locations = location_arr
instance.save()

当我这样做,我得到了


列位置的类型为jsonb [],但表达式为text []



LINE 1:... d= 2517,locations= ARRAY ['{loc ...



提示:您需要重写或转换表达式


所以我试图使用以下方式转储:

  import json 
location_arr = [{locations:loc1,amount:Decimal(100.00)},{locations :loc2,amount:Decimal(200.25)}]
instance.locations = json.dumps(location_arr)
instance.save()
/ pre>

然后我得到这个


LINE 1:... d = 2517,locations='[{loc:...



DETAIL:[必须引入显式指定的数组维度。

我正在使用:


  1. Django 1.9

  2. Python 2.7

  3. Postgres 9.4.10

  4. psycopg2 2.6.2


    1. 解决方案

      首先,让我们仔细看看这个重要的文本,从 Postgresql数组文档。


      提示:数组不是集合;搜索特定的数组元素可以是
      数据库设计的标志。考虑使用一个单独的表与
      行为每个项目将是一个数组元素。这将更容易
      进行搜索,并且可能会对大量的
      元素进行更好的扩展。






      现在我们在这里有什么?一个容纳json字段的数组。这是一个等待发生的灾难。请规范您的资料。


      I have a problem inserting to a field using ArrayField with JSONField inside.

      models.py

      locations = ArrayField(JSONField(null = True,blank = True), blank=True, null = True)
      

      Insert

      location_arr = [{"locations" : "loc1","amount":Decimal(100.00)},{"locations" : "loc2","amount":Decimal(200.25)}]
      instance.locations = location_arr
      instance.save()
      

      When I do this, I got

      column "locations" is of type jsonb[] but expression is of type text[]

      LINE 1: ...d" = 2517, "locations" = ARRAY['{"loc...

      Hint: You will need to rewrite or cast the expression.

      So I tried to dump it using:

      import json
      location_arr = [{"locations" : "loc1","amount":Decimal(100.00)},{"locations" : "loc2","amount":Decimal(200.25)}]
      instance.locations = json.dumps(location_arr)
      instance.save()
      

      then I got this

      LINE 1: ...d" = 2517, "locations" = '[{"loc":...

      DETAIL: "[" must introduce explicitly-specified array dimensions.

      I am using:

      1. Django 1.9
      2. Python 2.7
      3. Postgres 9.4.10
      4. psycopg2 2.6.2

      解决方案

      First of all, let's take a close look at this important text from the Postgresql Arrays document.

      Tip: Arrays are not sets; searching for specific array elements can be a sign of database misdesign. Consider using a separate table with a row for each item that would be an array element. This will be easier to search, and is likely to scale better for a large number of elements.

      Most of the time, you should not be using arrays. The same goes for JSONB. If you find yourself searching inside JSONB fields all the time the above statement is equally valid for JSONB.

      Now what do we have here? A an array that holds json field. This is a diaster waiting to happen. Please normalize your data.

      这篇关于Django JSONField内部的ArrayField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆