【发布时间】:2010-12-23 23:47:55
【问题描述】:
所以我有一个带有用于 Excel 电子表格的 FileField 的模型。我需要在此电子表格中添加另一列,在每一行中让用户从下拉列表中选择,然后保存并以 html 显示。所有的挑选和上传都将通过管理界面进行。所以我想出了如何在 html 中显示电子表格的方法,但是我不知道如何编写这个保存方法。我真的可以使用一些提示和技巧..
【问题讨论】:
标签: python html django excel django-admin
所以我有一个带有用于 Excel 电子表格的 FileField 的模型。我需要在此电子表格中添加另一列,在每一行中让用户从下拉列表中选择,然后保存并以 html 显示。所有的挑选和上传都将通过管理界面进行。所以我想出了如何在 html 中显示电子表格的方法,但是我不知道如何编写这个保存方法。我真的可以使用一些提示和技巧..
【问题讨论】:
标签: python html django excel django-admin
当我需要在 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
【讨论】: