相关疑难解决方法(0)

ArrayField中的Django JSONField

我在使用带有JSONField的ArrayField插入字段时遇到问题.

models.py

locations = ArrayField(JSONField(null = True,blank = True), blank=True, null = True)
Run Code Online (Sandbox Code Playgroud)

插入

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

当我这样做时,我得到了

列"locations"的类型为jsonb [],但表达式的类型为text []

第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()
Run Code Online (Sandbox Code Playgroud)

然后我得到了这个

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

详细信息:"["必须引入显式指定的数组维度.

我在用:

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

python arrays django postgresql json

14
推荐指数
2
解决办法
5355
查看次数

标签 统计

arrays ×1

django ×1

json ×1

postgresql ×1

python ×1