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. JavaScript中的AOP編程的基本實(shí)現(xiàn)2. PHP VS ASP3. Django框架安裝及項(xiàng)目創(chuàng)建過(guò)程解析4. Spring security 自定義過(guò)濾器實(shí)現(xiàn)Json參數(shù)傳遞并兼容表單參數(shù)(實(shí)例代碼)5. Django實(shí)現(xiàn)任意文件上傳(最簡(jiǎn)單的方法)6. java結(jié)構(gòu)性模式之變壓器模式介紹(二)7. 多級(jí)聯(lián)動(dòng)下拉選擇框,動(dòng)態(tài)獲取下一級(jí)8. Python max函數(shù)中key的用法及原理解析9. Python 中random 庫(kù)的詳細(xì)使用10. Spring注入Date類型的三種方法總結(jié)
