[英]django malformed array literal:
I have a little problem:我有一个小问题:
I have this model:我有这个模型:
class myModel(models.Model):
myField =JSONField()
I want to update this field:我想更新这个字段:
data={'rda': {'punti': 0, 'rank': 1, 'pos': 'eq'}}
a =myModel()
a.myField=data
a.save()
but I have this error:但我有这个错误:
Traceback (most recent call last): File "/home/hy0/.conda/envs/ciclods_env/lib/python3.7/site-packages/django/db/backends/utils.py", line 85, in _execute return self.cursor.execute(sql, params) psycopg2.DataError: malformed array literal: "{"rda": {"punti": 0, "rank": 1, "pos": "eq"}}"
回溯(最近一次调用):文件“/home/hy0/.conda/envs/ciclods_env/lib/python3.7/site-packages/django/db/backends/utils.py”,第 85 行,在 _execute 中返回 self .cursor.execute(sql, params) psycopg2.DataError:格式错误的数组文字:“{”rda”:{“punti”:0,“rank”:1,“pos”:“eq”}}”
how can I solve it?我该如何解决?
You must need to use PostgreSQL.您必须需要使用 PostgreSQL。 I tried so far, it works for me
我到目前为止尝试过,它对我有用
data = {
'rda':{
'punti':0,
'rank':1,
'pos':'eq'
}
}
test = myModel()
test.myField=data
test.save()
You need an instance of myModel
in order to store this.您需要一个
myModel
实例来存储它。 Eg something like:例如:
myModel.objects.create(myField={'rda': {'punti': 0, 'rank': 1, 'pos': 'eq'}})
I suspect there's some extra code in there that's not obvious, data
is a dict
but then you're calling save
on it which normally raises an AttributeError
.我怀疑那里有一些不明显的额外代码,
data
是一个dict
但是然后你调用它的save
通常会引发一个AttributeError
。 Also I'm guessing in your original code myModel
is not an instance of myModel
but rather the model itself and so you can't operate against it directly, you need an instance of it that represents the database row.此外,我在你的原码猜
myModel
不是一个实例myModel
而模型本身,所以你不能反对它直接操作,需要表示数据库行的一个实例。
Once you have an instance of myModel
, you can do:拥有
myModel
的实例后,您可以执行以下操作:
a_model = myModel()
a_model.myField = {'rda': {'punti': 0, 'rank': 1, 'pos': 'eq'}}
a_model.save()
Just to make this clear in my answer too, you need to ensure the database field matches the one declared in your model, check you have generated migrations and applied them for all your latest changes.只是为了在我的回答中明确这一点,您需要确保数据库字段与模型中声明的字段匹配,检查您是否已生成迁移并将它们应用于所有最新更改。
I had this exact issue and this post is the only reference I could find to it.我有这个确切的问题,这篇文章是我能找到的唯一参考。 I'm not sure what the cause is, but I finally fixed it by removing my migration scripts for a fresh database.
我不确定原因是什么,但我最终通过删除我的新数据库的迁移脚本来修复它。
If you're hitting this error, try removing the last few migration scripts, until you find the culprit.如果遇到此错误,请尝试删除最后几个迁移脚本,直到找到罪魁祸首。 Alternatively, you can start fresh like me and it should work again.
或者,您可以像我一样重新开始,它应该会再次工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.