What is the best way to store numerical data as a model in Django? I did look at What is the most efficent way to store a list in the Django models?, but I'd be curious to hear an answer tailored to numpy arrays as well.
Asked
Active
Viewed 695 times
1 Answers
4
One way would be to convert the numpy array to a string and store it in a textfield
base64.encodestring(nparray)
Another way would be to dump an array into a file and storing the path of the text file in the database
nparray.dump(file)
If you want to store the data in a structured manner in Django, you will need to create models to do that.
You could use 2 Models:
class Element(models.Model):
Value = models.FloatField()
Array = models.ForeignKey(Array)
class Array(models.Model):
#Not required, just for illustration, use the id models instead
Name = models.CharField('Name', max_length=100)
Parent = models.ForeignKey(self, blank=True, null=True)
You store the values in the Element model and create the structure using the Array model.
Say you have a 2D array you can store it this way [Array1, Array2, Array3] Array1 = [1,2,3] Array2 = [4,5,6] Array3 = [7,8,9]
Array('ParnetArray')
Array('Array1','ParentArray'),Array('Array2','ParentArray'),Array('Array3','ParentArray')
Element(1,'Array1'),Element(2,'Array1'),Element(3,'Array1'),Element(4',Array2'),Element(5,'Array2')...........

sj7
- 1,291
- 1
- 12
- 26
-
Is a textfield the "right" way to do things, though? If I used a textfield, presumably I would not be able to add data to the list with the Django admin panel. – Pythontology Sep 27 '14 at 14:32