【问题标题】:Most efficient Django model to store numerical data存储数值数据的最有效 Django 模型
【发布时间】:2014-11-22 09:26:37
【问题描述】:

在 Django 中将数值数据存储为模型的最佳方式是什么?我确实看过What is the most efficent way to store a list in the Django models?,但我很想听到一个针对numpy数组的答案。

【问题讨论】:

    标签: django


    【解决方案1】:

    一种方法是将 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')...........
    

    【讨论】:

    • 文本字段是“正确”的做事方式吗?如果我使用文本字段,大概我将无法使用 Django 管理面板将数据添加到列表中。
    • 非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2019-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-09
    • 2019-10-28
    • 1970-01-01
    • 2015-04-30
    相关资源
    最近更新 更多