通过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()) #创建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:\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另存为实际文件.而不是直接将其分配给该字段,您应该调用该字段的save方法并将其传递给:
gj = GeojsonDatua(dname = namevar)
gj.gdata.save('myfilename', cf)
参见the docs.
另请注意,如果您始终像这样创建gdata字段,则可能根本就不需要FileField.也许改用TextField.
以上这篇django 实现手动存储文件到model的FileField就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
更新日志
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】