﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
13809	FileField open method is only accepting 'rb' modes	Cesar Canassa	Chris Sinchok	"I have the following model in my application:

{{{
#!python
class OrdemServico(models.Model):
    carrinho = models.OneToOneField('Carrinho')
    criacao = models.DateTimeField(u'data de criação', default=datetime.now)
    pdf = models.FileField(upload_to=""ordem_pdf"")
}}}

Then I have the following code in my application:

{{{
#!python
>>> ordem, created = OrdemServico.objects.get_or_create(carrinho=self)
>>> ordem.pdf.open(mode='wb')
>>> f = ordem.pdf.file
>>> print f
c:\users\cesar\documents\projetos\rm2\mysite\media\ordem_pdf\157_5.pdf
>>> print f.tell()
0
>>> print f.write('abc')
Traceback (most recent call last):
  File ""C:\Python26\lib\site-packages\django\core\servers\basehttp.py"", line 280, in run
    self.result = application(self.environ, self.start_response)
  File ""C:\Python26\lib\site-packages\django\core\servers\basehttp.py"", line 674, in __call__
    return self.application(environ, start_response)
  File ""C:\Python26\lib\site-packages\django\core\handlers\wsgi.py"", line 241, in __call__
    response = self.get_response(request)
  File ""C:\Python26\lib\site-packages\django\core\handlers\base.py"", line 142, in get_response
    return self.handle_uncaught_exception(request, resolver, exc_info)
  File ""C:\Python26\lib\site-packages\django\core\handlers\base.py"", line 100, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File ""C:\Python26\lib\site-packages\django\contrib\admin\options.py"", line 239, in wrapper
    return self.admin_site.admin_view(view)(*args, **kwargs)
  File ""C:\Python26\lib\site-packages\django\utils\decorators.py"", line 76, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File ""C:\Python26\lib\site-packages\django\views\decorators\cache.py"", line 69, in _wrapped_view_func
    response = view_func(request, *args, **kwargs)
  File ""C:\Python26\lib\site-packages\django\contrib\admin\sites.py"", line 190, in inner
    return view(request, *args, **kwargs)
  File ""C:\Python26\lib\site-packages\django\utils\decorators.py"", line 21, in _wrapper
    return decorator(bound_func)(*args, **kwargs)
  File ""C:\Python26\lib\site-packages\django\utils\decorators.py"", line 76, in _wrapped_view
    response = view_func(request, *args, **kwargs)
  File ""C:\Python26\lib\site-packages\django\utils\decorators.py"", line 17, in bound_func
    return func(self, *args2, **kwargs2)
  File ""C:\Python26\lib\site-packages\django\contrib\admin\options.py"", line 991, in changelist_view
    response = self.response_action(request, queryset=cl.get_query_set())
  File ""C:\Python26\lib\site-packages\django\contrib\admin\options.py"", line 749, in response_action
    response = func(self, request, queryset)
  File ""C:\Users\Cesar\Documents\Projetos\RM2\mysite\..\mysite\moveit\admin.py"", line 41, in gerar_pdf
    carrinho.gera_ordem()
  File ""C:\Users\Cesar\Documents\Projetos\RM2\mysite\..\mysite\moveit\models.py"", line 204, in gera_ordem
    print f.write('abc')
IOError: [Errno 9] Bad file descriptor
}}}

I did some debugging and I find out that the open() method was opening the file in read-only mode. This was causing the IOError during the write call.

I checked the django.db.models.fields.file.py file:

{{{
#!python
    def _get_file(self):
        self._require_file()
        if not hasattr(self, '_file') or self._file is None:
            self._file = self.storage.open(self.name, 'rb')
        return self._file

    def open(self, mode='rb'):
        self._require_file()
        self.file.open(mode)
    # open() doesn't alter the file's contents, but it does reset the pointer
    open.alters_data = True
}}}

We I called the open() method it tried to access the file property which itself also tries to open the file, but in read-only mode.

I kinda solved the problem by calling the storage method directly:
{{{
#!python
    def open(self, mode='rb'):
        self._require_file()
        #self.file.open(mode)
        self._file = self.storage.open(self.name, mode)
}}}

Thanks,
Cesar Canassa"	Bug	closed	File uploads/storage	dev	Normal	fixed		real.human@… David Foster	Ready for checkin	1	0	0	0	0	0
