我想創建一個系統,使用戶能夠上傳zipfile,然後使用post_save信號提取它。當設置正確的文件路徑django信號,如何使用「實例」
class Project:
....
file_zip=FileField(upload_to='projects/%Y/%m/%d')
@receiver(post_save, sender=Project)
def unzip_and_process(sender, **kwargs):
#project_zip = FieldFile.open(file_zip, mode='rb')
file_path = sender.instance.file_zip.path
with zipfile.ZipFile(file_path, 'r') as project_zip:
project_zip.extractall(re.search('[^\s]+(?=\.zip)', file_path).group(0))
project_zip.close()
unzip_and_process
法正常工作(在這種情況下,我需要提供instance.file_zip.path
,但我不能讓/與信號設置的實例。有關信號Django文檔不清晰?並沒有例子所以,我該怎麼辦
我認爲這是** ** kwargs',我還不知道。你的例子很好,謝謝。 –
@Ferdinand在django文檔中沒有關於post_save的示例。 – Anuj
@Anuj - 我從來沒有說過有關'post_save'的任何例子。有一些關於如何使用信號的例子,這些也適用於'post_save',因爲這個特定的信號沒有什麼特別之處。 –