Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 内容文件未保存在Django模型文件字段中_Python_Django_Django Models_Django Views - Fatal编程技术网

Python 内容文件未保存在Django模型文件字段中

Python 内容文件未保存在Django模型文件字段中,python,django,django-models,django-views,Python,Django,Django Models,Django Views,在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.d

在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
尝试读取数据的代码:

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)


另外请注意,如果您总是这样创建
gdata
字段,您可能根本不需要文件字段;也许可以使用文本字段。

事实上,问题是您创建的文件没有名称:
ContentFile(,name=None)

在这种情况下,数据库将存储一个空字符串值
(“”)
,并且磁盘上不会存储任何文件。FieldFile的工作原理是:没有名称?没有文件。


因此,您需要在创建文件时提供一个名称:
ContentFile(,name=)

这对我来说很有效,谢谢