简体   繁体   English

django 格式错误的数组文字:

[英]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.

相关问题 Postgresql 插入多列格式错误的数组文字 - Postgresql insert multiple columns malformed array literal Python:插入字符串时格式错误的数组文字 - Python: malformed array literal when inserting a string ast.literal_eval() 格式错误的节点或字符串,同时转换带有 array() 列表的字符串 - ast.literal_eval() malformed node or string while converting a string with list of array()s ast.literal_eval ValueError('格式错误的字符串') - ast.literal_eval ValueError('malformed string') Django-传递数组时,以int()为基数10:','的无效文字 - Django - invalid literal for int() with base 10: ',' when passing an array literal_eval返回格式错误的字符串(给定的字符串不带引号) - literal_eval returns malformed string given a string without quote Python的literal_eval说字符串'a,b,c'格式错误 - Python's literal_eval says string 'a,b,c' is malformed 带有元组字符串表示的格式错误的字符串 ValueError ast.literal_eval() - Malformed String ValueError ast.literal_eval() with String representation of Tuple python:ast.literal_eval()提供ValueError:格式错误的字符串 - python: ast.literal_eval() gives ValueError: malformed string ValueError:使用ast.literal_eval时格式错误的字符串 - ValueError: malformed string when using ast.literal_eval
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM