国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

django 實(shí)現(xiàn)手動(dòng)存儲(chǔ)文件到model的FileField

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

通過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í),我遇到了問題,因?yàn)槊慨?dāng)我嘗試取回?cái)?shù)據(jù)時(shí),都會(huì)給我一個(gè)ValueError(“屬性沒有關(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)

參見the docs.

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

以上這篇django 實(shí)現(xiàn)手動(dòng)存儲(chǔ)文件到model的FileField就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Django
相關(guān)文章:
主站蜘蛛池模板: 垣曲县| 洛隆县| 石城县| 芜湖县| 合作市| 福州市| 郑州市| 罗定市| 高清| 塘沽区| 海林市| 延寿县| 大邑县| 敦化市| 嘉峪关市| 靖边县| 锡林郭勒盟| 白玉县| 琼海市| 万安县| 林芝县| 斗六市| 苏尼特右旗| 奇台县| 黑山县| 青海省| 长寿区| 四平市| 临颍县| 东安县| 双柏县| 张北县| 德保县| 朝阳市| 中牟县| 石楼县| 乐安县| 宣恩县| 南通市| 顺昌县| 登封市|