django 實(shí)現(xiàn)手動(dòng)存儲(chǔ)文件到model的FileField
通過(guò)POST請(qǐng)求,上傳了文件,想要將文件存儲(chǔ)在模型的FileField中
request.FILES中的值均為UploadedFile類文件對(duì)象
表單上傳的文件對(duì)象存儲(chǔ)在類字典對(duì)象request.FILES中,表單格式需為multipart/form-data
FieldFile.save(name, content, save=True)
name:命名文件名
content:必須是django.core.files.File或django.core.files.base.ContentFile二者之一的一個(gè)實(shí)例
from django.core.files.base import ContentFile#from django.core.files import Filephoto=request.FILES.get(’file’,’’)user=UserProfile.objects.get(id=uid)if photo: file_content = ContentFile(photo.read()) #創(chuàng)建ContentFile對(duì)象 #file_content = File(photo.read()) #創(chuàng)建File對(duì)象 user.photo.save(photo.name, file_content) #保存文件到user的photo域 user.save()
補(bǔ)充知識(shí):python-ContentFile未保存在Django模型FileField中
在我的Django模型中將字符串另存為文件時(shí),我遇到了問(wèn)題,因?yàn)槊慨?dāng)我嘗試取回?cái)?shù)據(jù)時(shí),都會(huì)給我一個(gè)ValueError(“屬性沒(méi)有關(guān)聯(lián)的文件”).
詳細(xì)信息如下:
模型:
class GeojsonData(models.Model):dname = models.CharField(max_length=200, unique=True)gdata = models.FileField(upload_to=’data’)def __str__(self): return self.dname
保存數(shù)據(jù)的代碼:
cf = ContentFile(stringToBeSaved)gj = GeojsonDatua(dname = namevar, gdata = cf)gj.save()
嘗試讀取數(shù)據(jù)的代碼:
def readGeo(data): f = GeojsonData.objects.all().get(id=data.id).gdata f.open(mode =’rb’) geo = f.read() return geo
追溯:
File 'C:PythonPython36-32libsite-packagesdjangocorehandlersexception.py' in inner41. response = get_response(request)
File 'C:PythonPython36-32libsite-packagesdjangocorehandlersbase.py' in _get_response187. response = self.process_exception_by_middleware(e, request)
File 'C:PythonPython36-32libsite-packagesdjangocorehandlersbase.py' in _get_response185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File 'C:PythonPython36-32libsite-packagesdjangocontribauthdecorators.py' in _wrapped_view23. return view_func(request, *args, **kwargs)
File 'C:appviews.py' in mapa80. geostr = app.readGeo.readGeo(d)
File 'C:appreadGeo.py' in readGeo6. f.open(mode =’rb’)
File 'C:PythonPython36-32libsite-packagesdjangodbmodelsfieldsfiles.py' in open80. self._require_file()
File 'C:PythonPython36-32libsite-packagesdjangodbmodelsfieldsfiles.py' in _require_file46. raise ValueError('The ’%s’ attribute has no file associated with it.' % self.field.name)
Exception Type: ValueError at /app/map/1Exception Value: The ’gdata’ attribute has no file associated with it.
解決方法:
您需要將ContentFile另存為實(shí)際文件.而不是直接將其分配給該字段,您應(yīng)該調(diào)用該字段的save方法并將其傳遞給:
gj = GeojsonDatua(dname = namevar)gj.gdata.save(’myfilename’, cf)
參見(jiàn)the docs.
另請(qǐng)注意,如果您始終像這樣創(chuàng)建gdata字段,則可能根本就不需要FileField.也許改用TextField.
以上這篇django 實(shí)現(xiàn)手動(dòng)存儲(chǔ)文件到model的FileField就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python爬蟲(chóng)實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊2. Ajax返回值類型與用法實(shí)例分析3. 如何在jsp界面中插入圖片4. 詳解盒子端CSS動(dòng)畫(huà)性能提升5. 使用FormData進(jìn)行Ajax請(qǐng)求上傳文件的實(shí)例代碼6. HTML 絕對(duì)路徑與相對(duì)路徑概念詳細(xì)7. asp批量添加修改刪除操作示例代碼8. .NET6打包部署到Windows Service的全過(guò)程9. 解決ajax請(qǐng)求后臺(tái),有時(shí)收不到返回值的問(wèn)題10. css代碼優(yōu)化的12個(gè)技巧
