Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing an Integer Array in a Django Database

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

like image 265
Christian Avatar asked Sep 15 '09 19:09

Christian


People also ask

How does Django store data in database?

Creating objectsTo create an object, instantiate it using keyword arguments to the model class, then call save() to save it to the database. This performs an INSERT SQL statement behind the scenes. Django doesn't hit the database until you explicitly call save() . The save() method has no return value.

Which Django model fields is used for integer value?

IntegerField is a integer number represented in Python by a int instance. This field is generally used to store integer numbers in the database. The default form widget for this field is a NumberInput when localize is False or TextInput otherwise.

What is Array field in Django?

ArrayField. A field for storing lists of data.


1 Answers

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)   
    ....
like image 122
Nabeel Ahmed Avatar answered Sep 30 '22 19:09

Nabeel Ahmed