【问题标题】:Django save methodDjango保存方法
【发布时间】:2010-12-23 23:47:55
【问题描述】:

所以我有一个带有用于 Excel 电子表格的 FileField 的模型。我需要在此电子表格中添加另一列,在每一行中让用户从下拉列表中选择,然后保存并以 html 显示。所有的挑选和上传都将通过管理界面进行。所以我想出了如何在 html 中显示电子表格的方法,但是我不知道如何编写这个保存方法。我真的可以使用一些提示和技巧..

【问题讨论】:

    标签: python html django excel django-admin


    【解决方案1】:

    当我需要在 python 中生成 excel 文件时,我发现 xlwt 包很有用。自从我使用它以来,python excel 操作技术似乎有了一些进步。 http://www.python-excel.org/ 有很多选择,还有详细的教程。

    至于 Django 的保存方法,我想出了一种无需创建临时文件即可更新 FileField 的方法,但我不确定这是否是一个好方法。花了很多时间:

    from django.db import models
    from django.core.files.base import ContentFile
    
    class Entity(models.Model):
        file = models.FileField(upload_to='%Y/%m/%d',blank=True)
        ...
        def store_file(self, name, data):
            """ This leaves the Entity model unsaved.
            The code that calls this function should
            also call Entity.save() eventually."""
    
            self.file = ContentFile(data)
            self.file.name = name
    

    【讨论】:

    • 这不是我想要的。我想要的是 save 方法将另一列添加到电子表格的 html 中,并为该列中的每个单元格提供一个下拉列表以供选择,然后实际保存 html ..
    猜你喜欢
    • 2013-08-25
    • 2018-11-19
    • 2019-03-04
    • 2015-11-03
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    相关资源
    最近更新 更多