Django ArrayField。如何通过 django admin 保存嵌套的时间数组?

问题描述 投票:0回答:2

我几个月前就已经问过这个问题了。

问题很愚蠢,但我找不到解决方案。从简单表单文本字段保存嵌套时间数组的有效格式是什么?

我有这样的ArrayField:

schedule = ArrayField(
        ArrayField(
            ArrayField(
                models.TimeField(null=True),
            ),
            size=2,
            null=True,
        ),
        size=7,
        null=True,
        blank=True,
    )

当我尝试从 django admin 保存它时,例如:

((09:00, 09:00), (09:00, 09:00), (09:00, 09:00), (09:00, 09:00), (09:00, 9:00), (9:00, 9:00), (9:00, 9:00))

我遇到错误

Item 0 in the array did not validate: Item 0 in the array did not validate: Item 0 in the array did not validate: Enter a valid time.
Item 1 in the array did not validate: Item 0 in the array did not validate: Item 0 in the array did not validate: Enter a valid time.
Item 2 in the array did not validate: Item 0 in the array did not validate: Item 0 in the array did not validate: Enter a valid time.
Item 3 in the array did not validate: Item 0 in the array did not validate: Item 0 in the array did not validate: Enter a valid time.
Item 4 in the array did not validate: Item 0 in the array did not validate: Item 0 in the array did not validate: Enter a valid time.
Item 5 in the array did not validate: Item 0 in the array did not validate: Item 0 in the array did not validate: Enter a valid time.
Item 6 in the array did not validate: Item 0 in the array did not validate: Item 0 in the array did not validate: Enter a valid time.
Item 7 in the array did not validate: Item 0 in the array did not validate: Item 0 in the array did not validate: Enter a valid time.
Item 8 in the array did not validate: Item 0 in the array did not validate: Item 0 in the array did not validate: Enter a valid time.
Item 9 in the array did not validate: Item 0 in the array did not validate: Item 0 in the array did not validate: Enter a valid time.
Item 10 in the array did not validate: Item 0 in the array did not validate: Item 0 in the array did not validate: Enter a valid time.
Item 11 in the array did not validate: Item 0 in the array did not validate: Item 0 in the array did not validate: Enter a valid time.
Item 12 in the array did not validate: Item 0 in the array did not validate: Item 0 in the array did not validate: Enter a valid time.
Item 13 in the array did not validate: Item 0 in the array did not validate: Item 0 in the array did not validate: Enter a valid time.

我已经尝试过一些其他格式,例如: [["09:00", "09:00"]] 或 (('09:00', '09:00')) 或只是纯文本,例如“09:00” ,“09:00”但没有成功。

python django postgresql django-admin django-orm
2个回答
1
投票

您的

schedule
字段是一个 3 维数组,但您传递的是一个 2 维数组。

schedule
字段定义应为:

schedule = ArrayField(
    ArrayField(
        models.TimeField(null=True),
        size=2,
        null=True,
    ),
    size=7,
    null=True,
    blank=True,
)

此外,默认的 Django 小部件不支持嵌套数组。您必须创建自己的小部件。


0
投票

结束 @antoine-pinsard 的回答,我要补充一点,不可能保存像 Array(Array(SomeSimpleField)) 这样的字段 - 因为 django 的

from django.contrib.postgres.forms import SimpleArrayField 
表单不允许这样做。
所以解决方案很简单 - 我创建了自己的根 ArrayField 并将数组的分隔符指定为
;
:

class RootArrayField(ArrayField):
    def formfield(self, **kwargs):
        return super().formfield(
            delimiter=";",
            **kwargs,
        )

...

class CustomModel(models.Model):
    some_field = RootArrayField(
        ArrayField(models.DecimalField(...)),
        ...
    )

现在我可以填写该字段,例如:

12.33, 43,12; 334.54, 12.4

© www.soinside.com 2019 - 2024. All rights reserved.