Home > Net >  How to stock a list in a Django Model?
How to stock a list in a Django Model?

Time:08-16

I'm trying to stock a list in a Django Model like this :

class Search(models.Model):
    result = models.CharField(max_length=100,
                              null=True, blank=True)
    name = models.BooleanField(default=True)
    eotp = models.BooleanField(default=True)
    reference = models.BooleanField(default=True)
    date = models.BooleanField(default=True)
    contains = []

But I don't think that it's the best way to stock a list, because I have an error when i want to create an element (like this):

new_search = Search(result=result, name=name, eotp=eotp, reference=reference, date=date, contains=contains)

"Search() got an unexpected keyword argument 'contains'"

Do you know if there is a better way to stock lists in django models or if I have to do this in another way ?

Thanks

CodePudding user response:

You need to use django's ArrayField so that you can stock your list into your model.

Use this code for your model:



class Search(models.Model):
    result = models.CharField(max_length=100,
                              null=True, blank=True)
    name = models.BooleanField(default=True)
    eotp = models.BooleanField(default=True)
    reference = models.BooleanField(default=True)
    date = models.BooleanField(default=True)
    contains = ArrayField(models.CharField(max_length=200), blank=True) #---> Set as per your requirements

Documentation: https://docs.djangoproject.com/en/4.1/ref/contrib/postgres/fields/#arrayfield

  • Related