如何在django中保存到“uploaded_to”之前更改上传的文件?

时间:2013-07-01 16:50:11

标签: python django-models django-forms

我有一个这样的表格:

from django import forms
from manupulators import Replacer
class ContentForm(forms.Form):
    csvfile = forms.FileField( label='Select content file')
    txtfile = forms.FileField( label='Select changes file file')
    def clean(self):
        cleaned_data = super(DocumentForm, self).clean()
        csvfile = cleaned_data.get("csvdoc")
        textfile = cleaned_data.get("txtfile")

        if csvfile and  textfile:
            # Only do something if both fields are valid so far.
            """
            here goes nothing
            """
            worker = Replacer(csvfile,textfile)
            worker.open()
            worker.replace()
            worker.save() ##  how to do this i can alter the replacer to
                          ## return the changed stuff, how will it be saved to 
                          ## uploaded_to?

        return cleaned_data

Replacer类有上面调用的内部方法来修改上传的文本文件,我只想在调用它们上面的替换器之后保存这些文件,我坚持如何在修改它后代替保存的manupulated内容文件原始内容。

这是模型:

from django.db import models

class Document(models.Model):
    csvfile = models.FileField(upload_to='documents/%Y/%m/%d')
    txtfile = models.FileField(upload_to='documents/%Y/%m/%d')

1 个答案:

答案 0 :(得分:0)

结果我可以在视图中获得路径,

if request.method == 'POST':
        form = ContentForm(request.blah,request.blah)
        if form.is_valid():
            docs = Document(csvdoc = ...,txtfile = ...,
                     app_user=...,)
            docs.save()
            pathone =  docs.csvdoc.path
            pathtwo =  docs.txtfile.path # all done