Home > Blockchain >  Similar field to ArrayField in Django to use with SQL Server
Similar field to ArrayField in Django to use with SQL Server

Time:09-30

I want to connect my Django backend to SQL Server but one of my models require a an array field like the one from PostgreSQL so is there an alternative?

CodePudding user response:

If you are using SQLite as your database, there is no alternative for ManyToManyField.

  1. Here is the library for using Listfield into sql named django-my-sql, which supports a ListField.

But it shows as a textfield in admin panel.

if you enter data through admin panel then you have to separate your values by commas: name1,name2,name3.

CodePudding user response:

First, store the data in string or char field; then, you need to do a conversion on data for setting or getting.

Something like this:

class Sample(models.Model):
    _array_Field = models.CharField(max_length=100)

    @property
    def array_field(self):
        return self._array_Field.split(',')

    @array_field.setter
    def array_field(self, value: list):
       joined_value = ''
       for item in value:
           joined_value  = item   ','
       joined_value = joined_value[0:len(joined_value)-1]
       self._array_Field = joined_value
       self.save()
  • Related