Arrayfield in Django

10,458

In your case you are talking about 1-dim array, and it should be smth like that:

board = ArrayField(
    ArrayField(
        models.CharField(max_length=10, blank=True),
        size=8,
    ),
    size=1,
)

so in your case smth like:

pagerange_str_array = ArrayField(
    ArrayField(
        models.CharField(max_length=10, blank=True)
    ),
    size=1
)
Share:
10,458
Malik A. Rumi
Author by

Malik A. Rumi

Updated on June 04, 2022

Comments

  • Malik A. Rumi
    Malik A. Rumi almost 2 years

    my code:

    pagerange_str_array = ArrayField(models.CharField(max_length=10,
                                     blank=True, default=list()))
    

    docs example: https://docs.djangoproject.com/en/2.0/ref/contrib/postgres/fields/

    board = ArrayField(
        ArrayField(
            models.CharField(max_length=10, blank=True),
            size=8,
        ),
        size=8,
    )
    

    docs - size: This is an optional argument.

    error message:

    DETAIL: Array value must start with "{" or dimension information.
    

    But none of these work: default={}, '{}',or {''} They all get the same error message.

    docs: If you give the field a default, ensure it’s a callable such as list (for an empty default) or a callable that returns a list (such as a function)

    But none of these work either: default=list, list(). null=True (my original code) also does not work.

    All these examples give the same error. I have seen non-Django solutions on the web, but what is an example of code that works in Django?! Thanks.

    • Paulo Almeida
      Paulo Almeida about 6 years
      This is probably not the issue, but your default is in the CharField, not in the ArrayField.
    • Malik A. Rumi
      Malik A. Rumi about 6 years
      I've never seen it done that way, but perhaps a good tip. Thanks.
  • Malik A. Rumi
    Malik A. Rumi about 6 years
    Ok, that makes sense. I thought if there was nothing there about size, then by default the dimensions would match.