Search code examples
pythondjangopostgresqldjango-admindjango-orm

Django ArrayField. How to save nested array of time through django admin?


I already asked this question several months ago.

Question is stupid, but i cant find solution. What a valid format for saving nested array of time from simple form text field?

I have ArrayField like this:

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

When i am trying to save it from django admin like:

((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))

I'm getting errors

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.

I already tried some other formats like: [["09:00", "09:00"]] or (('09:00', '09:00')) or just plain text like "09:00", "09:00" but it did not work.


Solution

  • Your schedule field is a 3-dimensions array, but you are passing a 2-dimensions array.

    The schedule field definition should be:

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

    Also, the default Django widget does not support nested arrays. You will have to create your own widget.