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
--
You received this message because you are subscribed to the Google Groups
"Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-developers/fbac2558-164a-4906-b6e9-3d2c845b25a3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.