1

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.

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

This is right notice, but after changings in models.py I still can't save array and getting same error messages.
Thinking a bit further, it seems reasonable that django default form fields and/or widgets do not support nested arrays. You would have to create your own formfield/widget to handle your schedule field. Can you tell me your version of Django? I will try to write such widget to complete my answer.
0

concluding @antoine-pinsard answer, I will add, that it's impossible to save field like Array(Array(SomeSimpleField)) - because django's from django.contrib.postgres.forms import SimpleArrayField forms doesn't allow it.
So the solution is simple - I've created own root ArrayField and specified arrays' delimiter as ;:

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

...

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

And now I'm able to fill the field like: 12.33, 43,12; 334.54, 12.4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.