最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

django 實現(xiàn)手動存儲文件到model的FileField

 更新時間:2020年03月30日 10:31:46   作者:Hayley-L  
這篇文章主要介紹了django 實現(xiàn)手動存儲文件到model的FileField,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

通過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 File


photo=request.FILES.get('file','')
user=UserProfile.objects.get(id=uid)
if photo: 
 file_content = ContentFile(photo.read()) #創(chuàng)建ContentFile對象
 #file_content = File(photo.read()) #創(chuàng)建File對象
 user.photo.save(photo.name, file_content) #保存文件到user的photo域
 user.save()

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

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

詳細信息如下:

模型:

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:\Python\Python36-32\lib\site-packages\django\core\handlers\exception.py" in inner
41. response = get_response(request)

File "C:\Python\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
187. response = self.process_exception_by_middleware(e, request)

File "C:\Python\Python36-32\lib\site-packages\django\core\handlers\base.py" in _get_response
185. response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Python\Python36-32\lib\site-packages\django\contrib\auth\decorators.py" in _wrapped_view
23. return view_func(request, *args, **kwargs)

File "C:\app\views.py" in mapa
80. geostr = app.readGeo.readGeo(d)

File "C:\app\readGeo.py" in readGeo
6. f.open(mode ='rb')

File "C:\Python\Python36-32\lib\site-packages\django\db\models\fields\files.py" in open
80. self._require_file()

File "C:Python\Python36-32\lib\site-packages\django\db\models\fields\files.py" in _require_file
46. raise ValueError("The '%s' attribute has no file associated with it." % self.field.name)

Exception Type: ValueError at /app/map/1
Exception Value: The 'gdata' attribute has no file associated with it.

解決方法:

您需要將ContentFile另存為實際文件.而不是直接將其分配給該字段,您應(yīng)該調(diào)用該字段的save方法并將其傳遞給:

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

參見the docs.

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

以上這篇django 實現(xiàn)手動存儲文件到model的FileField就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

文成县| 唐河县| 绥江县| 德钦县| 邯郸县| 石狮市| 灵璧县| 扬中市| 朔州市| 盖州市| 视频| 新兴县| 汶川县| 临沂市| 响水县| 虎林市| 油尖旺区| 屏山县| 邓州市| 房山区| 德州市| 黔西县| 潞西市| 永靖县| 遵义市| 沧源| 巴南区| 阳信县| 会泽县| 柏乡县| 中牟县| 普定县| 周至县| 鄂托克旗| 湖口县| 宣汉县| 邵东县| 濮阳县| 务川| 剑川县| 营口市|