将字典列表作为 JSON 二进制插入 peewee 模型

inserting list of dicts as JSON binary into peewee model

我有一个简单的字典列表,我想将其插入我的 postgres 数据库中的 LabelModel table

l = [{'label': 'A',
  'x': 132.56338500976562,
  'y': 333.7539367675781,
  'width': 183.78598022460938,
  'height': 404.6580505371094,
  'score': 0.9848693609237671},
 {'label': 'B',
  'x': 179.97842407226562,
  'y': 367.101318359375,
  'width': 127.43386840820312,
  'height': 59.047882080078125,
  'score': 0.965998113155365},
 {'label': 'C',
  'x': 431.1368408203125,
  'y': 365.9712219238281,
  'width': 127.59616088867188,
  'height': 60.77362060546875,
  'score': 0.9622131586074829}]
class TblLabelByAI(BaseModel):
  name = pw.TextField()
  labels = BinaryJSONField()
  modified_at = pw.DateField(default=datetime.datetime.utcnow)
q = {"imagename":"testname","labels":l}
TblLabelByAI.get_or_create(**q)

有什么原因让我得到以下信息:

ProgrammingError: operator does not exist: jsonb = record
LINE 1: ...testname') AND ("t1"."labels" = (CAST('{...
                                     ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.

皮威:3.14.9
Python3.8

@coleifer - 顺便说一句,很棒的工作

解决方法如下:

from psycopg2.extras import Json

data = [{'x': 123, 'y': 123},{'x': 123, 'y': 123},{'x': 123, 'y': 123}]
data = Json(data)