- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在将 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/
我想使用数组字段来存储代表特定自定义类的 json 字符串列表。我所做的是: from django.contrib.postgres.fields import JSONField, ArrayFi
是否可以为 ArrayField 添加默认值? 我尝试为电子邮件字段执行此操作,但这不起作用: 常量.py: ORDER_STATUS_CHANGED = 'order_status_changed'
我正在使用 Django Postgres ArrayField response_headers = ArrayField(models.TextField(blank=True),blank=Tr
我有一个简单的模型 class Texts(models.Model): instructions_top_char instructions_top_char = ArrayFiel
我正在将 PostgreSQL 与 Django 一起使用,并且正在尝试使用 ArrayField(CharField()) 存储值和检索都不会引发任何异常,但尝试存储 ["string", "ano
我有以下错误信息: AttributeError: 'module' object has no attribute 'ArrayField' 相关代码段如下: from __future__ imp
我有一些用户帐户的字段为“donation_methods”。用户可以选择不同的方式来接受捐赠。默认情况下,每个用户都应该在数组中具有值“cash” donation_methods = ArrayF
ArrayField 的当前管理小部件是一个字段,以逗号作为分隔符,如下所示(文本列表): 这并不理想,因为我会有更长的文本(甚至 20 个单词)并包含逗号。我可以 change the delimi
我有一个类型 ArrayField在一个模型中。 my_list 的建议元素列表中的后端自动地。但是,用户随后需要根据需要更新此列表。该模型如下所示: class my_model(models.Mo
我正在使用 Django 的 postgres ArrayField。 假设我有这样一个模型: from django.db import models from django.contrib.pos
为了解决我的(有点特殊的)问题,我发现我必须使用 Django ArrayField: https://docs.djangoproject.com/en/2.1/ref/contrib/postgr
在我的 Django 应用程序中,使用 Postgresql,我有一个带有 CharFields 的 ArrayField 的模型。我想知道是否有一种数据库方法来聚合并获取表中所有字符串的列表。例如:
对于我数据库中的模型,我需要为特定字段存储大约 300 个值。如果我使用特定于 Postgres 的 ArrayField,在性能 和查询简单性 方面会有什么缺点?而不是具有一对多关系的单独表? 最佳
我需要配置一个 django.contrib.postgres.fields.ArrayField,其中第一个元素是 float ,第二个元素是一个小的正整数: data = [[1.23, 3],
我正在尝试更改 Django 为 ArrayField 生成的默认错误消息(特别是输入了太多项的错误消息) 如果用户在我的 ArrayField 中输入了太多项目,则会在模板中生成以下消息: List
我的模型看起来像这样: from django.contrib.postgres.fields import ArrayField class Trigger(models.Model): s
下面的案例 [{type: x, val: y}, {...}, {...}, ...] 可以单独用 JSONField 表示。 还有下面的案例 [1,2,3,4,5] 可以用 JSONField 表
我有一个通常看起来像这样的模型: class CategoryModel(models.Model): categories = ArrayField(..) 假设我有两个类别,“catego
我有一个定义的模型 from django.contrib.postgres.fields import ArrayField class Model(models.Model): name
我有一个定义的模型 from django.contrib.postgres.fields import ArrayField class Model(models.Model): name
我是一名优秀的程序员,十分优秀!