[英]How to check if a new file was selected in django admin using a pre_save signal?
[英]Is there a way to convert the uploaded excel file to CSV using django pre_save signal?
我想知道是否可以使用 django pre_save 信號將用戶選擇的 excel 文件轉換為 CSV。 假設用戶上傳了“test.xlsx”,我想使用 pre_save 信號自動轉換該文件。 用戶上傳文件后,它實際上應該是我的 FileField 中的“test.csv”。
是的,您可以在pre_save
中執行此操作,但最好僅在處理上傳的 Django 表單或 DRF 序列化程序中執行此操作。 這樣,您可以將轉換錯誤作為驗證錯誤等冒泡給用戶。
假設 model 中的字段為upload
,您將使用
clean_upload(self, file)
。validate_upload(self, file)
。 只需從其中一個返回一個新的ContentFile()
以及轉換后的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.