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

umuewwlo  于 2024-01-07  发布在  PostgreSQL
关注(0)|答案(2)|浏览(92)

我在几个月前已经问过这个问题。
问题是愚蠢的,但我不能找到解决方案。什么是有效的格式,节省嵌套数组的时间从简单的形式文本字段?
我有这样的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”,但它不工作。

wrrgggsh

wrrgggsh1#

您的schedule字段是一个三维数组,但您传递的是一个二维数组。
schedule字段定义应为:

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

字符串
另外,默认的Django小部件不支持嵌套数组。你必须创建自己的小部件。

rjee0c15

rjee0c152#

在@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

相关问题