24

What is the best way to store an array of integers in a django database?

6 Answers 6

19

I'm using ArrayField: https://docs.djangoproject.com/en/2.1/ref/contrib/postgres/fields/#querying-arrayfield

eg.

class Place(models.Model):
    nearby_places_ids = ArrayField(models.IntegerField(null=True, blank=True), null=True, blank=True)

with usage:

place.nearby_places_ids = [1,2,3]
place.save()

models.Place.objects.filter(nearby_places_ids__contains=[1])
<QuerySet [<Place: Hostel DIC>]>

models.Place.objects.filter(nearby_places_ids__contains=[1,2,3,4])
<QuerySet []>
Sign up to request clarification or add additional context in comments.

1 Comment

SQLite does not support this Field
17

(only for Django with Postgres DB)

You can use ArrayField with base_field IntegerField

A field for storing lists of data. Most field types can be used, and you pass another field instance as the base_field. You may also specify a size. ArrayField can be nested to store multi-dimensional arrays.

ArrayField(
    models.IntegerField(),
    blank=True,  # Optional: allows the array to be empty
    default=list  # Default value is an empty list
)

From 2018

CommaSeparatedIntergerField is no more available since Django 1.9:

From Docs:

Deprecated since version 1.9: This field is deprecated in favor of CharField with validators=[validate_comma_separated_integer_list].


By default it sets a comma separated integer list field.

int_list_validator

Returns a RegexValidator instance that ensures a string consists of integers separated by sep. It allows negative integers when allow_negative is True.

from django.db import models
from django.core.validators import int_list_validator


class YourModel(models.Model):
    ....
    ....
    int_list = models.CharField(validators=int_list_validator)   
    ....

1 Comment

It should be something like models.CharField(validators=[int_list_validator], max_length=100)
9

CommaSeparatedIntegerField comes to mind instantly. It is implemented as VARCHAR on most database backends. To be sure though, you may want to skim through django/db/backends/*.

2 Comments

That basically means that I have to convert the string back into integers myself?
This is super dated information. The current dev (nor the current release: v1.11) site that you linked, doesn't mention a CommaSeparatedIntegerField.
6

See this answer for a description of a possible alternative version of CommaSeparatedIntegerField that would do more of the work for you (converting from list to string and back). Maybe you could check in with that guy about whether he wrote it already :-)

Comments

5

If you're using PostgreSQL, you can store array types natively.

See for example: https://github.com/aino/django-arrayfields and https://github.com/ecometrica/django-dbarray

Comments

1

Use ArrayField . Here's the syntax :

from django.contrib.postgres.fields import ArrayField
class SampleModel(models.Model):
     my_integer_array =  ArrayField(
        models.IntegerField(),
         default= [1,1,0,0], blank=False,null = False, size = 4)

To store a complete array :

SampleModel.my_integer_array = [3,4,5,6]
SampleModel.save()

To store/update a single element :

SampleModel.my_integer_array[2] =7
SampleModel.save()

To fetch the entire array :

my_variable = SampleModel.my_integer_array

To fetch a single element :

e = SampleModel.my_integer_array[3]

Note : ArrayField can only be used when you are using postgreSQL as the database

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.