Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.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
AttributeError:str没有属性名称-django python_Python_Django_Django Models - Fatal编程技术网

AttributeError:str没有属性名称-django python

AttributeError:str没有属性名称-django python,python,django,django-models,Python,Django,Django Models,我正在尝试从url读取图像并保存到db中 image = Image() name = urlparse(imgurl).path.split('/')[-1] image.bild.save(name, File(urllib2.urlopen(imgurl).read()), save=False)#error line image.von_location = location image.save() 这是我的图像模型 class Image(models.Model): von

我正在尝试从url读取图像并保存到db中

image = Image()
name = urlparse(imgurl).path.split('/')[-1]
image.bild.save(name, File(urllib2.urlopen(imgurl).read()), save=False)#error line
image.von_location = location
image.save()
这是我的图像模型

class Image(models.Model):
   von_location= models.ForeignKey(Location,related_name="locations_image",default=0)
   bild = models.ImageField(upload_to=locationimage,default='')
   def __unicode__(self):
       return self.bild.name
当我试图调用image file的
save()
方法时,出现了下面的错误

AttributeError: str has no attribute name
name
只是我在这里读到的图像的名称

这是错误消息的屏幕截图


我能够用下面的测试应用程序在Django 1.4上重现错误。基本上,您需要使用
ContentFile
而不是
文件,因为您正在读取图像的内容。如果试图将文件对象直接传递到
文件
,将遇到未知大小的错误

基本测试应用程序:

models.py

class TestModel(models.Model):
    file = models.FileField(upload_to="test")

    def __unicode__(self):
        return self.file.name
tests.py

import os.path
import urllib2
from urlparse import urlparse

from django.test import TestCase
from django.core.files import File
from django.core.files.base import ContentFile

from testapp.models import TestModel

class SimpleTest(TestCase):
    def test_models(self):
        test_model = TestModel()
        imgurl = 'http://www.stackoverflow.com/favicon.ico'
        name = urlparse(imgurl).path.split('/')[-1]
        content =  urllib2.urlopen(imgurl).read()
        #test_model.file.save(name, File(content), save=False) # error line
        test_model.file.save(name, ContentFile(content), save=False)
        test_model.save()
        print test_model

我能够用下面的测试应用程序在Django 1.4上重现错误。基本上,您需要使用
ContentFile
而不是
文件,因为您正在读取图像的内容。如果试图将文件对象直接传递到
文件
,将遇到未知大小的错误

基本测试应用程序:

models.py

class TestModel(models.Model):
    file = models.FileField(upload_to="test")

    def __unicode__(self):
        return self.file.name
tests.py

import os.path
import urllib2
from urlparse import urlparse

from django.test import TestCase
from django.core.files import File
from django.core.files.base import ContentFile

from testapp.models import TestModel

class SimpleTest(TestCase):
    def test_models(self):
        test_model = TestModel()
        imgurl = 'http://www.stackoverflow.com/favicon.ico'
        name = urlparse(imgurl).path.split('/')[-1]
        content =  urllib2.urlopen(imgurl).read()
        #test_model.file.save(name, File(content), save=False) # error line
        test_model.file.save(name, ContentFile(content), save=False)
        test_model.save()
        print test_model

我能够用下面的测试应用程序在Django 1.4上重现错误。基本上,您需要使用
ContentFile
而不是
文件,因为您正在读取图像的内容。如果试图将文件对象直接传递到
文件
,将遇到未知大小的错误

基本测试应用程序:

models.py

class TestModel(models.Model):
    file = models.FileField(upload_to="test")

    def __unicode__(self):
        return self.file.name
tests.py

import os.path
import urllib2
from urlparse import urlparse

from django.test import TestCase
from django.core.files import File
from django.core.files.base import ContentFile

from testapp.models import TestModel

class SimpleTest(TestCase):
    def test_models(self):
        test_model = TestModel()
        imgurl = 'http://www.stackoverflow.com/favicon.ico'
        name = urlparse(imgurl).path.split('/')[-1]
        content =  urllib2.urlopen(imgurl).read()
        #test_model.file.save(name, File(content), save=False) # error line
        test_model.file.save(name, ContentFile(content), save=False)
        test_model.save()
        print test_model

我能够用下面的测试应用程序在Django 1.4上重现错误。基本上,您需要使用
ContentFile
而不是
文件,因为您正在读取图像的内容。如果试图将文件对象直接传递到
文件
,将遇到未知大小的错误

基本测试应用程序:

models.py

class TestModel(models.Model):
    file = models.FileField(upload_to="test")

    def __unicode__(self):
        return self.file.name
tests.py

import os.path
import urllib2
from urlparse import urlparse

from django.test import TestCase
from django.core.files import File
from django.core.files.base import ContentFile

from testapp.models import TestModel

class SimpleTest(TestCase):
    def test_models(self):
        test_model = TestModel()
        imgurl = 'http://www.stackoverflow.com/favicon.ico'
        name = urlparse(imgurl).path.split('/')[-1]
        content =  urllib2.urlopen(imgurl).read()
        #test_model.file.save(name, File(content), save=False) # error line
        test_model.file.save(name, ContentFile(content), save=False)
        test_model.save()
        print test_model


返回self.bild
返回什么?@karthikr,self.bild.name返回图像文件的名称,这是我的想法。您可以尝试
image.bild.save(name,file(urllib2.urlopen(imgurl)),save=False)
?urllib2.urlopen应该已经返回类似文件的对象。此外,堆栈跟踪似乎与列出的代码具有不同的变量名什么是
返回self.bild
返回?@karthikr,self.bild.name返回图像文件的名称,这是我的想法。您可以尝试
image.bild.save(name,file(urllib2.urlopen(imgull)),save=False)
?urllib2.urlopen应该已经返回类似文件的对象。此外,堆栈跟踪似乎与列出的代码具有不同的变量名什么是
返回self.bild
返回?@karthikr,self.bild.name返回图像文件的名称,这是我的想法。您可以尝试
image.bild.save(name,file(urllib2.urlopen(imgull)),save=False)
?urllib2.urlopen应该已经返回类似文件的对象。此外,堆栈跟踪似乎与列出的代码具有不同的变量名什么是
返回self.bild
返回?@karthikr,self.bild.name返回图像文件的名称,这是我的想法。您可以尝试
image.bild.save(name,file(urllib2.urlopen(imgull)),save=False)
?urllib2.urlopen应该已经返回类似文件的对象。此外,堆栈跟踪的变量名似乎与列出的代码名不同,谢谢,但是ContentFile只接受字符串,我认为图像在测试中存储正确。我相信基于
urlopen
请求中的标题,它会尝试对其进行适当的解码。在本例中,对于一个图像,它将是一个字节字符串,然后由
ContentFile
适当处理,因为它使用
StringIO
从字节字符串重建文件对象。谢谢,但ContentFile只接受字符串。我认为图像在测试中存储正确。我相信基于
urlopen
请求中的标题,它会尝试对其进行适当的解码。在本例中,对于一个图像,它将是一个字节字符串,然后由
ContentFile
适当处理,因为它使用
StringIO
从字节字符串重建文件对象。谢谢,但ContentFile只接受字符串。我认为图像在测试中存储正确。我相信基于
urlopen
请求中的标题,它会尝试对其进行适当的解码。在本例中,对于一个图像,它将是一个字节字符串,然后由
ContentFile
适当处理,因为它使用
StringIO
从字节字符串重建文件对象。谢谢,但ContentFile只接受字符串。我认为图像在测试中存储正确。我相信基于
urlopen
请求中的标题,它会尝试对其进行适当的解码。在本例中,对于图像,它将是一个字节字符串,然后由
ContentFile
适当处理,因为它使用
StringIO
从字节字符串重建文件对象。