簡體   English   中英

在django中更改文件名

[英]changing file name in django

我有這個型號......

class MyModel(models.Model):
    ...
    file = models.FileField(upload_to='files/',null=True, blank=True)
    ...

當我上傳文件時,示例文件名是docfile.doc 當我更改文件或我重寫它並再次上傳docfile.doc該文件將變為docfile_1.doc ,舊的docfile.doc仍然存在。

我正在django-admin上傳和保存數據

我的問題是,我怎么能去除老docfile.doc如果我上傳新docfile.doc和文件名仍然是docfile.doc

我可以幫助我嗎? 提前致謝

我試試這個:

def content_file_name(instance, filename):
    print instance
    print filename
    file = os.path.exists(filename)
    print file
    if file:
        os.remove(filename)
    return "file/"+str(filename)

 class MyModel(models.Model):
        ...
        file = models.FileField(upload_to=content_file_name,null=True, blank=True)
        ...

但沒有任何事情發生,當我再次上傳docfile.doc時,它將成為docfile_1.doc ,舊的docfile.doc仍然存在。

我不知道該怎么做,但我認為這些鏈接可以幫助你:

在這里,您可以找到FileField接受的兩個選項。 我認為最讓你感興趣的是FileField.storage 您可以在該參數中傳遞存儲對象。

它說:

FileField.storage:可選。 存儲對象,用於處理文件的存儲和檢索。

然后,如果您閱讀此內容,您會看到您可以編寫自己的存儲對象。 以下是有關如何操作的一些說明。 我認為您可以覆蓋_save方法以完成您想要執行的操作(即:如果文件已存在,請在保存新副本之前將其刪除。)

不過要小心! 我不知道你要存儲的文件的來源是哪一個。 也許,你的應用程序將收到許多具有相同名稱的文件,盡管它們都不同。 在這種情況下,您可能希望使用callable作為FileField.upload_to參數,以便為站點接收的每個文件確定唯一的文件名。

我希望這可以幫助你!

我明白了...我用它

def content_file_name(instance, filename):
    print instance
    print filename
    file = os.path.exists("media/file/"+str(filename))
    print file
    if file:
        os.remove("media/file/"+str(filename))
    return "file/"+str(filename)

您還可以看一下: ImageField覆蓋具有相同名稱的圖像文件

定義您自己的存儲並覆蓋其get available_name方法。

下一個代碼可以解決您的問題。 您可以覆蓋pre_save方法,其中image實際上已保存到存儲中。 請重命名項目的功能。 將新創建的圖像字段ImageFieldWithPermantNameupload_to函數(content_file_name)一起使用。

如果代碼太復雜,你可以簡化它。 我使用代碼執行更復雜的上傳圖像操作:我在自定義_save_image函數中即時創建縮略圖。 所以,你可以簡化它。

from PIL import Image
import StringIO
from django.db.models import ImageField
from django.db.models.fields.files import FileField
from dargent.settings import MEDIA_ROOT
import os

class ImageFieldWithPermanentName( ImageField ):
    def pre_save( self, model_instance, add ):
        file = super( FileField, self ).pre_save(model_instance, add)
        if file and not file._committed:
            if callable( self.upload_to ):
                path = self.upload_to( model_instance, "" )
            else:
                path = self.upload_to
            file.name = path # here we set the same name to a file
            path = os.path.join( MEDIA_ROOT, path )
            chunks = _get_chunks( file.chunks() )
            _save_image( chunks, path )

        return file     

def _get_chunks( chunks ):
    chunks_ = ""
    for chunk in chunks:
        chunks_ += chunk
    return chunks_

def _get_image( chunks ):
    chunks_ = ""
    for chunk in chunks:
        chunks_ += chunk

    virt_file = StringIO.StringIO( chunks_ )
    image = Image.open( virt_file )
    return image

def _save_image( chunks, out_file_path ):
    image = _get_image( chunks )
    image.save( out_file_path, "JPEG", quality = 100 )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM