Admin画面のFileFieldからアップロードした直後のファイルの処理がなかなか分からなかったが以下のようにmodels.pyにdef save(self, *args, **kwargs):を追加して解決しました。
例 models.py:
class DataFile(models.Model):
data = models.FileField()
def save(self, *args, **kwargs):
super(DataFile, self).save(*args, **kwargs)
# Do anything you'd like with the data in filename
filename = str(self.data)
filepath = os.path.join(settings.MEDIA_ROOT, filename)
with zipfile.ZipFile(filepath) as existing_zip:
existing_zip.extractall(os.path.dirname(filepath))
data = models.FileField()
def save(self, *args, **kwargs):
super(DataFile, self).save(*args, **kwargs)
# Do anything you'd like with the data in filename
filename = str(self.data)
filepath = os.path.join(settings.MEDIA_ROOT, filename)
with zipfile.ZipFile(filepath) as existing_zip:
existing_zip.extractall(os.path.dirname(filepath))
参考
Django admin file upload handling · Eviatar Bach
http://eviatarbach.com/2014/12/12/django-admin-upload-handling/
http://eviatarbach.com/2014/12/12/django-admin-upload-handling/
0 件のコメント:
コメントを投稿