FieldFile对象没有属性' startswith'

时间:2013-06-26 13:18:59

标签: python django

我正在测试一些选项。我使用django-storages和S3上传了一个文件,它运行正常。但是,当我尝试使用下面的代码读取文件时,我收到以下错误:

  

'FieldFile'对象没有属性'startswith'

为什么吗

即使default_storage.exists(self.filepath)也会出错。

 csv_file = default_storage.open(self.filepath, 'r')


 new_object = CSVImport(csvfile=csv_file.read(),
                        model=Contact,
                        modelspy=".",
                        mappings="1=first_name,2=mobile,3=email,4=last_name,5=43,6=16")

 new_object.run()

完整错误:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/contacts/upload/configurator/95/

Django Version: 1.5.1
Python Version: 2.7.2
Installed Applications:
('django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.humanize',
 'django.contrib.sitemaps',
 'south',
 'userena',
 'social_auth',
 'djcelery',
 'storages',
 'endless_pagination',
 'django.contrib.flatpages',
 'django_sagepay',
 'guardian',
 'widget_tweaks',
 'badger',
 'tastypie',
 'accounts',
 'contacts',
 'sms',
 'smartpages',
 'django_sagepay',
 'unsubscribe',
 'core',
 'django_nose',
 'debug_toolbar')
Installed Middleware:
('dogslow.WatchdogMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'async_messages.middleware.AsyncMiddleware',
 'core.middleware.ssl.SSLRedirect',
 'core.middleware.account.RefreshBalance',
 'debug_toolbar.middleware.DebugToolbarMiddleware')


Traceback:
File "/Users/user/Documents/workspace/t/django-env/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Users/user/Documents/workspace/t/django-env/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  25.                 return view_func(request, *args, **kwargs)
File "/Users/user/Documents/workspace/t/contacts/views.py" in upload_configurator
  201.             process_upload_Temp(upload_id=upload.id, cleaned_data=data)
File "/Users/user/Documents/workspace/t/contacts/views.py" in process_upload_Temp
  222.     upload.process(cleaned_data=cleaned_data)
File "/Users/user/Documents/workspace/t/contacts/models.py" in process
  169.         csv_file = default_storage.open(self.filepath, 'r')
File "/Users/user/Documents/workspace/t/django-env/lib/python2.7/site-packages/django/core/files/storage.py" in open
  36.         return self._open(name, mode)
File "/Users/user/Documents/workspace/t/django-env/lib/python2.7/site-packages/storages/backends/s3boto.py" in _open
  237.         name = self._normalize_name(self._clean_name(name))
File "/Users/user/Documents/workspace/t/django-env/lib/python2.7/site-packages/storages/backends/s3boto.py" in _clean_name
  204.         return os.path.normpath(name).replace('\\', '/')
File "/Users/user/Documents/workspace/t/django-env/lib/python2.7/posixpath.py" in normpath
  318.     initial_slashes = path.startswith('/')

Exception Type: AttributeError at /contacts/upload/configurator/95/
Exception Value: 'FieldFile' object has no attribute 'startswith'


I have tried:

csv_file.read() gives same error.

Full Error details:

2 个答案:

答案 0 :(得分:1)

错误是由:

引发的
csv_file = default_storage.open(self.filepath, 'r')

表示self.filepath的类型为FieldFile。所以鉴于它的名字,我希望该对象是一个已经打开的文件。所以你应该尝试:

new_object = CSVImport(csvfile=self.filepath.read(), ... )

但我可能错了,self.filepath可能是一个包含您要打开的文件路径的文件...鉴于您的代码示例很难说,我只能猜测。

答案 1 :(得分:0)

您正在将FieldFile对象传递给default_storage.open(),但它希望文件名为字符串。为其指定用于保存文件的名称,而不是FieldFile对象。