What is the best way to store an array of integers in a django 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.
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.
ArrayField. A field for storing lists of data.
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)
....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With