Django: How to add to an ArrayField?
Question:
I have some questions about the ArrayField mentioned here: https://docs.djangoproject.com/en/3.1/ref/contrib/postgres/fields/#django.contrib.postgres.fields.ArrayField
- How do you add to an ArrayField? It’s clear you can treat it like a regular field and do things like
my_array_field = []
my_array_field.save()
But what’s confusing is I’m seeing everyone mention my_array_field.append("something")
in StackOverflow questions. This isn’t mentioned in the documentation at all. Moreover, if an arrayfield is .append()
ed to, does it still require .save()?
Answers:
do we need to call save()
?
Yes, we need to call the save()
method to COMMIT the changes to Database. Unless calling the save()
method, the value only persists in the scope of the variable, as the python list. So, calling the save()
is a must do thing while updating the values.
how to add values to array field?
You can treat it as a normal python list object, because Django converts the db array field to native Python list
object
model_instance.my_array_field = [1,2,3]
model_instance.save()
model_instance.my_array_field += [4,5,6]
model_instance.save()
This isn’t mentioned in the documentation
Probably, they may be missed to include this information.
Django documentation ArrayField
recommends to add some default value, and make it callable, so just try in your models:
class SomeModel(models.Model):
...
my_array_field = ArrayField(models.CharField(max_length=255), default=list)
So you’ll be able to add items in it just like with regular list in python:
my_array_field.append("something")
my_array_field.append("another_something")
And even more – you can use all methods like pop, extend, index etc.
I have some questions about the ArrayField mentioned here: https://docs.djangoproject.com/en/3.1/ref/contrib/postgres/fields/#django.contrib.postgres.fields.ArrayField
- How do you add to an ArrayField? It’s clear you can treat it like a regular field and do things like
my_array_field = []
my_array_field.save()
But what’s confusing is I’m seeing everyone mention my_array_field.append("something")
in StackOverflow questions. This isn’t mentioned in the documentation at all. Moreover, if an arrayfield is .append()
ed to, does it still require .save()?
do we need to call
save()
?
Yes, we need to call the save()
method to COMMIT the changes to Database. Unless calling the save()
method, the value only persists in the scope of the variable, as the python list. So, calling the save()
is a must do thing while updating the values.
how to add values to array field?
You can treat it as a normal python list object, because Django converts the db array field to native Python list
object
model_instance.my_array_field = [1,2,3]
model_instance.save()
model_instance.my_array_field += [4,5,6]
model_instance.save()
This isn’t mentioned in the documentation
Probably, they may be missed to include this information.
Django documentation ArrayField
recommends to add some default value, and make it callable, so just try in your models:
class SomeModel(models.Model):
...
my_array_field = ArrayField(models.CharField(max_length=255), default=list)
So you’ll be able to add items in it just like with regular list in python:
my_array_field.append("something")
my_array_field.append("another_something")
And even more – you can use all methods like pop, extend, index etc.