0

I am trying to store a vector using Django in Sqlite3 using the following Model Field

vector = models.CharField(validators=[int_list_validator(sep=' ', allow_negative=True)], max_length=256)

and the corresponding Serializer Field

vector = serializers.CharField()

Before saving the instance the vector has a shape of (1,128) and on retrieval from database it isn't of required shape.

Is it the correct way of saving a vector or an alternate solution exists?

Thanks for your help!

Abhijith E
  • 27
  • 1
  • 3
  • 9
  • You should use binary BLOB type. How to use is here : https://stackoverflow.com/questions/4915397/django-blob-model-field, and why to use is here: https://stackoverflow.com/questions/4532879/how-to-save-array-of-integer-numbers-in-a-column-in-sql-server-2005 – furkanayd Dec 06 '19 at 04:58
  • Since you write `max_length=256`, are all your integers smaller than 100? – Kent Shikama Dec 06 '19 at 04:58
  • @KentShikama All the integers are Positive/Negative Decimals smaller than 100 – Abhijith E Dec 06 '19 at 05:02

2 Answers2

1

Use TextField instead of CharField with a length limit, or you can use BinaryField

Phan Dinh
  • 245
  • 2
  • 11
  • And what Serializer should I use for BinaryField? – Abhijith E Dec 13 '19 at 03:58
  • You should build your own binary field, then you can use `to_representation` to decode data from your database and `to_internal_value` to encode data before save to database, https://www.django-rest-framework.org/api-guide/fields/#custom-fields – Phan Dinh Dec 16 '19 at 06:45
0

If your data is not too large, you can use a JSON field too.

from django.db import models

def empty_list(): return []
value = models.JSONField(default=empty_list, blank=True, null=True)
mm_
  • 1,566
  • 2
  • 18
  • 37