I am currently using Django with PostgreSQL. I use django-extensions to run custom scripts, inserting data to database. I have a model as below:
class Route(models.Model):
"""
Bus routes of İzmir.
"""
code = models.PositiveSmallIntegerField(
unique=True,
primary_key=True,
verbose_name="Code"
)
stops = models.ManyToManyField(
Stop,
null=True,
blank=True,
related_name="routes",
verbose_name="Stops"
)
terminals = ArrayField(
models.CharField(
null=False,
blank=False,
max_length=32,
),
size=2,
default=[],
verbose_name="Terminals"
)
departure_times = ArrayField(
ArrayField(
models.TimeField(
null=False,
blank=False
),
null=True, # This was added later to maybe solve the problem.
default=[]
),
default=[],
size=6,
verbose_name="Departure Times"
)
class Meta:
verbose_name = "Route"
verbose_name_plural = "Routes"
ordering = ["code"]
As you can see, I have embedded another ArrayField to departure_times. My data has irregular shapes as mentioned in Django documentation as a note. So, naturally, it raises the error as below:
django.db.utils.DataError: multidimensional arrays must have array expressions with matching dimensions
As mentioned in documentation as "the underlying field should be made nullable", I added null = True to my embedded ArrayField. However, I did not understand the following statement in the note in documentation:
the values padded with None
The questions on my head are
1) Will I deal with those data, modifying the length of irregular lists as below?
[a, b, c, None, None]
[a, b, None, None, None]
[a, b, c, d, e]
2) Is there a built-in way to overcome DataError?
If you think seeing the script would help you to solve the problem, here it is.
Environment
- python 3.5.1
- django 1.9.7
- psycopg2 2.6.2
- postgresql 9.5