亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

django 實現手動存儲文件到model的FileField

瀏覽:2日期:2024-10-16 15:46:46

通過POST請求,上傳了文件,想要將文件存儲在模型的FileField中

request.FILES中的值均為UploadedFile類文件對象

表單上傳的文件對象存儲在類字典對象request.FILES中,表單格式需為multipart/form-data

FieldFile.save(name, content, save=True)

name:命名文件名

content:必須是django.core.files.File或django.core.files.base.ContentFile二者之一的一個實例

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()) #創建ContentFile對象 #file_content = File(photo.read()) #創建File對象 user.photo.save(photo.name, file_content) #保存文件到user的photo域 user.save()

補充知識:python-ContentFile未保存在Django模型FileField中

在我的Django模型中將字符串另存為文件時,我遇到了問題,因為每當我嘗試取回數據時,都會給我一個ValueError(“屬性沒有關聯的文件”).

詳細信息如下:

模型:

class GeojsonData(models.Model):dname = models.CharField(max_length=200, unique=True)gdata = models.FileField(upload_to=’data’)def __str__(self): return self.dname

保存數據的代碼:

cf = ContentFile(stringToBeSaved)gj = GeojsonDatua(dname = namevar, gdata = cf)gj.save()

嘗試讀取數據的代碼:

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另存為實際文件.而不是直接將其分配給該字段,您應該調用該字段的save方法并將其傳遞給:

gj = GeojsonDatua(dname = namevar)gj.gdata.save(’myfilename’, cf)

參見the docs.

另請注意,如果您始終像這樣創建gdata字段,則可能根本就不需要FileField.也許改用TextField.

以上這篇django 實現手動存儲文件到model的FileField就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Django
相關文章:
主站蜘蛛池模板: 成人资源在线 | 精品国产成人a区在线观看 精品国产成人a在线观看 | 午夜拍拍| 在线观看免费黄色 | 亚洲免费影院 | 一区二区三区四区视频在线 | 国产成人久久一区二区三区 | 国产乳摇福利视频在线观看 | 超h福利视频在线观看 | 国产免费一区二区三区最新 | 免费黄色一级网站 | 2048国产精品原创综合在线 | 国产高清不卡一区二区 | 日本亚洲精品无码专区 | 久久九九综合 | 91精品国产综合久久欧美 | 成人满18在线观看网站免费 | a成人在线 | 一级特黄牲大片免费视频 | 一级黄色录像大片 | 日韩中文字幕免费在线观看 | 综合亚洲欧美日韩一区二区 | 欧美日韩中文字幕一区二区高清 | 最新国语露脸精品国产 | 九九99久久精品在免费线bt | 国产午夜a理论毛片在线影院 | 国产精品一国产精品免费 | 日本黄色网页 | 91福利国产在线观看香蕉 | 国产视频一区二区在线播放 | 91不卡在线精品国产 | 久久成人免费播放网站 | 亚洲国产精品久久综合 | 亚洲精品一区二区乱码在线观看 | 一级国产黄色片 | 国产tv在线 | 欧美综合中文字幕久久 | ccmm123在线播放 | 亚洲欧美日韩中文综合在线不卡 | 日本一级毛片免费 | 免费大片黄在线观看日本 |