Use case:
class Obj(models.Model):
file_field = models.FileField(upload_to="test")
...
obj = Obj()
obj.file_field.save(filename, content)
...
with obj.file_field.open() as a_file:
a_file.read()
Any reason I shouldn't make a ticket and patch for this?
- Martin Chase
Sounds like a good idea to me - go ahead!
Jacob
--
You received this message because you are subscribed to the Google Groups "Django developers" group.
To post to this group, send email to django-d...@googlegroups.com.
To unsubscribe from this group, send email to django-develop...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.
It isn't alreayd?!?! All file objects django exposes should be context managers, I thought I added that to a base mixin class a while ago, but apparently I didn't add it to the right thing.
FieldFile inherits from File, though, so the methods are present. You
just have to do:
with obj.file_field as f:
...
instead of calling the open() method (which returns None), as the OP
tried. The only catch is that the __enter__ method does not open the
file, so if you close the file and then try the above, you'll get an
exception.