gpt4 book ai didi

python - ArrayField 返回错误值

转载 作者:行者123 更新时间:2023-11-29 13:25:44 25 4
gpt4 key购买 nike

我正在将 PostgreSQL 与 Django 一起使用,并且正在尝试使用

ArrayField(CharField())

存储值和检索都不会引发任何异常,但尝试存储 ["string", "another_string", "string with whitespaces", "str"] 然后检索返回

'{string,another_string,"string with whitespaces",str}'

使用 ArrayField(IntegerField())ArrayField(ArrayField(CharField())) 时不会出现此问题

虽然我知道我可以只使用 JSON 或将列表嵌套在另一个列表中以获得可以正确读取的 [[strings]],但我想知道为什么会这样。


编辑:事实证明,使用 ArrayField(ArrayField(CharField())) 也不起作用:

Python 3.3.2 (default, Mar 20 2014, 20:25:51) 
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from api.models import Game
>>> g = Game.objects.all()[0]
>>> g.p1hand = [["a", "b", "c d"]]
>>> g.p1hand
[['a', 'b', 'c d']]
>>> g.save()
>>> g = Game.objects.all()[0]
>>> g.p1hand
'{{a,b,"c d"}}'
>>>

我不知道为什么它在 ArrayField(ArrayField(CharField())) 的单个实例中工作


编辑:关于@LongBeard_Boldy,这是 ArrayField(ArrayField(CharField())) 返回的另一个实例:

>>> g.game_objects
[['Test', '3', '3', '0', 'building', '5', '2', '2', '0'], ....]

最佳答案

我遇到了同样的问题,最终成为迁移问题。我确实在 models.py 文件中包含了该字段,但我没有迁移,所以 django 有点了解如何处理列表,但不知道如何检索它们。

一旦我迁移了数据库,一切就完美了。

关于python - ArrayField 返回错误值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34154130/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com