0

This is the list I want to store in my database :

[30, 9.45, 
[3.4499999999999993, 1.0500000000000007], 
[10.625, 12.875], 
[14.075, 11.825], 
[9.0, 15.0], 
[12.904858299595142, 15.637651821862349], 
[15.0, 18.0], 
[1.0, 1.0], 
[6.0, 10.5], 
[10.5, 6.0], 
[0.0, 0.0], 
[[3, 6], [3, 6, 6]], 
[[6.0, 1.5], [10.5, 6.0, 4.5]], 
[27.0, 94.5], 
[110.1141194331984, 106.83476720647775]]

my models.py :

D240_TOTAL = ArrayField(
            ArrayField(
                models.FloatField(max_length=10, blank=True),
                blank=True, null=True,
            ),
            blank=True, null=True,
        )

but am getting the error :

Field 'D240_TOTAL' expected a number but got [3, 6].

because my field is 2-d array but i am giving him a 3d array

my edited field :

M_BC_TOTAL = ArrayField(
                ArrayField(

                    ArrayField(
                        models.FloatField(max_length=10, blank=True),
                        blank=True, null=True,
                    ), blank=True,null=True),
                blank=True, null=True,
        )

but am getting this error : can't adapt type 'numpy.int32'

3
  • 1
    change your code so that you store a 2D array, or add another ArrayField, or use a JSONField to store any list Commented Jul 8, 2021 at 13:47
  • @Louis-JustinTallot i tried to add another Arrayfield but i am getting this error : can't adapt type 'numpy.int32' you can take a look at my edited question Commented Jul 8, 2021 at 13:54
  • the problem here is that your array is heterogeneous : [1, [2, 3] , [[4, 5], [6, 7]]] so Django cannot pass a number as an array. You should use JSONField (cf my answer below) Commented Jul 8, 2021 at 13:58

1 Answer 1

1

You can change you Model to use a JSONField instead. This will allow you to store any list :

from django.db import models
from django.contrib.postgres.fields import JSONField

D240_TOTAL = JSONField()

See also here for an how-to of JSONField.

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

1 Comment

don't forget to run migrations after you changed your model !

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.