FileField binary/text mode

76 views
Skip to first unread message

Martin Tiršel

unread,
Aug 23, 2014, 5:01:47 PM8/23/14
to django-d...@googlegroups.com
Hello,

today, I wanted to use FileField directly in Python CSV module, but problem was, that CSV module requires the file to be opened in text mode, not binary. So I though that this will be sufficient:

    obj.my_file_field.file.close()
    obj
.my_file_field.file.open(mode='r')
    csvreader
= csv.reader(obj.my_file_field)
   
for row in csvreader:
       
pass

But Django is internally working with BytesIO (https://github.com/django/django/blob/1.7c3/django/core/files/base.py#L98), so I get an Exception if I try to iterate the csvreader (that iterates the file object).

I am forced to use something like this:

    with open(obj.my_file_field.file.name, 'r') as fp:
        csvreader
= csv.reader(obj.my_file_field)
       
for row in csvreader:
           
pass

Should not the FileField handle such cases? For example by specifying the mode I wan to use in model field attributes?

Thanks,
Martin
Reply all
Reply to author
Forward
0 new messages