Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

282
Visualizações
Is it possible to store an array in Django model?

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 CharFields 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.

over 4 years ago · Santiago Trujillo
5 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório

0

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.

over 4 years ago · Santiago Trujillo Relatório

0

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().

over 4 years ago · Santiago Trujillo Relatório

0

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).

over 4 years ago · Santiago Trujillo Relatório

0

you can store a json and good to go with sub arrays of that JSON:

if (data != "attachmentto") {
        formData.append(data, this.recipe[data])
        console.log('rec data ', data)}
        else if (data == "attachmentto") {
          console.log('rec data434 ', this.recipe.attachmentto)
          var myObj = { name: this.recipe.attachmentto.name, age: 31, city: "New York" };
          let kokos = JSON.stringify(myObj);
          // this.recipe.attachmentto.name = this.recipe.attachmentto.name
          formData.append('attachmentto', kokos)
        }

Django backend:

class Video(models.Model):
  objects = models.Manager()
  title = models.CharField(max_length=80)
  description = models.TextField(max_length=300)
  picture = JSONField(encoder=None)
  price = models.IntegerField(default=0)
  url = models.URLField()
  category = models.CharField(max_length=50)
  subcategory = models.TextField(max_length=50)
  attachmentto = JSONField(encoder=None)
  # attachmentto2 = models.JSONField()
  user = models.ForeignKey(User, on_delete=models.CASCADE, default=1)

and result on backend API:

   {
        "id": 174,
        "title": "ads",
        "description": "ads",
        "picture": {
            "name": "https://v3.vuejs.org/logo.png"
        },
        "price": 0,
        "user": 1,
        "rating_average": 0,
        "attachmentto": {
            "age": 31,
            "city": "New York",
            "name": [
                "https://restdj.herokuapp.com/media/uploads/ftf_3_cJ0V7TF.png",
                "https://restdj.herokuapp.com/media/uploads/ftf_3.jpg"
            ]
        }
    },

I call it noicely(nicely). Notice that we send a JSON and we have a array in that JSON

Kokos is the full JSON disagned for djangoo:

  var myObj = { name: this.recipe.attachmentto.name, age: 31, city: "New York" };



let kokos = JSON.stringify(myObj);

formData.append('attachmentto', kokos)

Above; name: this.recipe.attachmentto.name is an array

Here is the array: "name": [

"https://restdj.herokuapp.com/media/uploads/ftf_3_cJ0V7TF.png",
                "https://restdj.herokuapp.com/media/uploads/ftf_3.jpg"
            ]
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda