I was wondering if it's possible to store an array in a Django model?
I'm asking this because I need to store an array of int
(e.g [1,2,3]) in a field and then be able to search a specific array and get a match with it or by it's possible combinations.
I was thinking to store that arrays as strings in CharField
s and then, when I need to search something, concatenate the values(obtained by filtering other model) with '[', ']' and ',' and then use a object filter with that generated string. The problem is that I will have to generate each possible combination and then filter them one by one until I get a match, and I believe that this might be inefficient.
So, I hope you can give me other ideas that I could try.
I'm not asking for code, necessarily, any ideas on how to achieve this will be good.
Creating objects To 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.
Models inheritance works the same way as normal Python class inheritance works, the only difference is, whether we want the parent models to have their own table in the database or not. When the parent model tables are not created as tables it just acts as a container for common fields and methods.
Django's JSONField actually stores the data in a Postgres JSONB column, which is only available in Postgres 9.4 and later. JSONField is great when you want a more flexible schema. For example if you want to change the keys without having to do any data migrations, or if not all your objects have the same structure.
A model in Django is supposed to correlate to a table in the database, so that each field in the model will actually be a field in the table for that model. To achieve what you're trying to do, you need to create a second model, which will be the object you want to store.
To display the Array field in Django Admin, a Form for the field must be present. Since the array is made up of abstract Author models, the form can be easily created by using a ModelForm. If you do not specify a ModelForm for your array models in the model_form_class argument, Djongo will automatically generate a ModelForm for you.
What is ArrayField in Django? ArrayField is Field that represents a column in the PostgreSQL database. It’s used to store data arrays in one model instance without needing an OneToMany relationship. ArrayField can represent any Field type except ForeignKey, OneToOneField, and ManyToManyField.
A model is the single, definitive source of information about your data. It contains the essential fields and behaviors of the data you’re storing. Generally, each model maps to a single database table. Each model is a Python class that subclasses django.db.models.Model. Each attribute of the model represents a database field.
With Djongo there can be an array of embedded documents inside the parent document. You can create an embed array/list of models inside the parent model and store it directly into MongoDB. class ArrayField ( MongoField ): def __init__ ( self, model_container: typing. Type [ Model ], model_form_class: typing. Type [ forms.
I'd have two advices for you:
1) Use ArrayField
if you are using PostgreSQL as your database. You can read more about ArrayField
here.
2) Encode your array as JSON and store it either as a plain string or using a JSONField
as found here.
I'd personally prefer option number 1 since that is the cleaner and nicer way but depending on what you are actually using to store your data that might not be available to you.
Yes, you can use it like this:
from django.contrib.postgres.fields import ArrayField
class Board(models.Model):
pieces = ArrayField(ArrayField(models.IntegerField()))
However, it can only be available when using PostgreSQL for the database.
If you aren't using Postgres, I recommend Django's validate_comma_separated_integer_list
validator.
https://docs.djangoproject.com/en/dev/ref/validators/#django.core.validators.validate_comma_separated_integer_list
You use is as a validator on a CharField()
.
I don't know why nobody has suggested it, but you can always pickle things and put the result into a binary field.
The advantages of this method are that it will work with just about any database, it's efficient, and it's applicable to more than just arrays. The downside is that you can't have the database run queries on the pickled data (not easily, anyway).
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