相关疑难解决方法(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
查看次数

Django Postgres ArrayField vs One-to-Many关系

对于我的数据库中的模型,我需要为特定字段存储大约300个值.如果我使用Postgres特定的ArrayField而不是具有One-to-Many关系的单独表,那么在查询性能简单性方面会有什么缺点?

django postgresql django-models

12
推荐指数
1
解决办法
1498
查看次数

标签 统计

django ×2

postgresql ×2

arrays ×1

django-models ×1

json ×1

python ×1