【发布时间】:2014-11-22 09:26:37
【问题描述】:
在 Django 中将数值数据存储为模型的最佳方式是什么?我确实看过What is the most efficent way to store a list in the Django models?,但我很想听到一个针对numpy数组的答案。
【问题讨论】:
标签: django
在 Django 中将数值数据存储为模型的最佳方式是什么?我确实看过What is the most efficent way to store a list in the Django models?,但我很想听到一个针对numpy数组的答案。
【问题讨论】:
标签: django
一种方法是将 numpy 数组转换为字符串并将其存储在文本字段中
base64.encodestring(nparray)
另一种方法是将数组转储到文件中并将文本文件的路径存储在数据库中
nparray.dump(file)
如果你想在 Django 中以结构化的方式存储数据,你需要创建模型来做到这一点。
您可以使用 2 个模型:
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)
您将值存储在 Element 模型中并使用 Array 模型创建结构。
假设你有一个二维数组,你可以这样存储它 [阵列1,阵列2,阵列3] 数组 1 = [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')...........
【讨论】: