在formset中上载相同的图像文件将返回ValueError

时间:2013-02-16 09:41:27

标签: django django-forms

我一直在使用django 1.4制作一个用户可以用来上传一张或多张图片的套餐。

formset用于向导步骤,但手动处理done方法中的is_valid check和formset.save()。

formset表现得非常奇怪,当我在formset中选择不同的图像时,它可以很好地工作。但是,当我在两个字段中上传相同的图像时,我收到以下错误

ValueError at /item/post-project/

I/O operation on closed file

这很奇怪,我不确定如何调试这样的问题,我尝试使用一个函数来替换文件字段中的upload_to参数,以便始终使用uuid字段,因为文件命名重复,它会抛出此错误,但它没有解决问题。

这是我的功能

AttachmentFormSet   = inlineformset_factory(Item, Attachment, extra=attachment_form_extra, max_num=8, can_delete=False, form=AttachmentForm)
formset = AttachmentFormSet(request.POST, request.FILES, instance=instance,)
if formset.is_valid():
  formset.save()

有关如何解决此问题的任何建议?

错误堆栈

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/item/post-project/

Django Version: 1.4.3
Python Version: 2.7.2
Installed Applications:
('admin_tools',
 'admin_tools.theming',
 'admin_tools.menu',
 'admin_tools.dashboard',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs',
 'social_auth',
 'account',
 'item',
 'sponsor',
 'pages',
 'misc.helper')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  111.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  20.                 return view_func(request, *args, **kwargs)
File "/Users/mo/Projects/pythonic/yrok-env/yrok/yrok/apps/item/views.py" in post_item
  105.     return response(request)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/views/generic/base.py" in view
  48.             return self.dispatch(request, *args, **kwargs)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/contrib/formtools/wizard/views.py" in dispatch
  223.         response = super(WizardView, self).dispatch(request, *args, **kwargs)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
  69.         return handler(request, *args, **kwargs)
File "/Users/mo/Projects/pythonic/yrok-env/yrok/yrok/apps/item/wizards.py" in post
  109.                 return self.render_done(form, **kwargs)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/contrib/formtools/wizard/views.py" in render_done
  328.         done_response = self.done(final_form_list, **kwargs)
File "/Users/mo/Projects/pythonic/yrok-env/yrok/yrok/apps/item/wizards.py" in done
  156.         formset.save()
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/forms/models.py" in save
  497.         return self.save_existing_objects(commit) + self.save_new_objects(commit)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/forms/models.py" in save_new_objects
  628.             self.new_objects.append(self.save_new(form, commit=commit))
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/forms/models.py" in save_new
  731.             obj.save()
File "/Users/mo/Projects/pythonic/yrok-env/yrok/yrok/apps/item/models.py" in save
  327.         super(Attachment, self).save(force_insert=force_insert, force_update=force_update, using=using)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/db/models/base.py" in save
  463.         self.save_base(using=using, force_insert=force_insert, force_update=force_update)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/db/models/base.py" in save_base
  551.                 result = manager._insert([self], fields=fields, return_id=update_pk, using=using, raw=raw)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/db/models/manager.py" in _insert
  203.         return insert_query(self.model, objs, fields, **kwargs)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/db/models/query.py" in insert_query
  1593.     return query.get_compiler(using=using).execute_sql(return_id)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in execute_sql
  911.         for sql, params in self.as_sql():
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/db/models/sql/compiler.py" in as_sql
  872.                 for obj in self.query.objs
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/db/models/fields/files.py" in pre_save
  249.             file.save(file.name, file, save=False)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/db/models/fields/files.py" in save
  86.         self.name = self.storage.save(name, content)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/core/files/storage.py" in save
  45.         name = self._save(name, content)
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/core/files/storage.py" in _save
  195.                         for chunk in content.chunks():
File "/Users/mo/Projects/pythonic/yrok-env/lib/python2.7/site-packages/django/core/files/base.py" in chunks
  63.             self.seek(0)

Exception Type: ValueError at /item/post-project/
Exception Value: I/O operation on closed file

0 个答案:

没有答案