Opened 5 years ago

Closed 8 months ago

#26367 closed Cleanup/optimization (needsinfo)

Assess if FieldFile can work with stdlib File instead of requiring Django's File

Reported by: Cristiano Coelho Owned by: David Smith
Component: File uploads/storage Version: 1.9
Severity: Normal Keywords: file storage
Cc: Cristiano Coelho Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tim Graham)

The FileField docs ( state that in order to save a file into a FileField (through FieldFile proxy) you need a django File object and not a regular one.

Looking at the code ( ) it seems like the only reason to have a File object is because it needs the 'size' property which isn't available on every file-like object.
Looking into the same file, it doesn't seem anyone uses _size anymore, only its parent class, BUT, this class actually overrides the size property so it is not calling the parent class .size (and if it did, it would cache it).

Would it be possible to remove any _size from FieldFile and then test against regular file-like objects rather than "File" and if everything works change the docs?
The only other place a File is required is at FileSystemStorage._save, however, since you don't call ._save but instead .save which already converts any file into a File (and it is also stated in the docs that the method can accept any file), the requierement for a File seems unnecessary.

There's also a topic here!topic/django-developers/CjoPZq8lZms that might suggest that the storage and file field classes require a few improvements.

Change History (11)

comment:1 Changed 5 years ago by Cristiano Coelho

Summary: Minor FieldFile (proxy for FieldFile) improvement?Minor FieldFile (proxy for FileField) improvement?

comment:2 Changed 5 years ago by Tim Graham

Description: modified (diff)
Summary: Minor FieldFile (proxy for FileField) improvement?Assess if FieldFile can work with stdlib File instead of requiring Django's File
Triage Stage: UnreviewedAccepted

The _size removal looks okay to me: PR

Not sure about whether this solves the issue of not requiring Django's File subclass, but accepting for further investigation.

comment:3 Changed 5 years ago by Tim Graham <timograham@…>

In f15f4b8b:

Refs #26367 -- Removed obsolete _size cache on FieldField.

The _size attribute is used in File.size but FieldFile overrides it.

comment:4 Changed 5 years ago by Cristiano Coelho

Hey Tim, thanks for the quick change for _size.

The requirement of File might be even more difficult to confirm, even if it is not a requirement for the django implementation, what if other people's own storage relies on it? Still there doesn't seem to be any checks for File on the save method so it is either blowing up already for people using normal files or everyone is guarded with a File object. So maybe as long as it works fine with django we shouldn't worry about other people's storages.

It seems like the File object is required at FieldFile for its open method and size property which is not present on regular files. However, if the method handled the conversion to File of content, it should work fine from there, removing the requierement that the .save method is always called with a File object. Other requierements of File would still apply ( for all the storage class methods) but it would definetly make saving files less verbose.

Looking at the save method is it possible that it also has an issue? After saving, the current 'cached' file instance is never refrshed, only the file name of the instance:

comment:5 Changed 5 years ago by Tim Graham

I don't have any motivation to do further work on this ticket. Feel free to investigate and submit patches if you like. It would be helpful if you motivated the issue a bit more clearly (what is the use case, e.g. why is using Django's File a problem).

comment:6 Changed 5 years ago by Collin Anderson

Could we just have the implementation wrap it if needed before sending it to the storage backend?

comment:7 Changed 5 years ago by Cristiano Coelho

Would it be as easy as wrap it in the save method of FieldFile? Note that the storage would still be required to return a File object on retrieval.
That simple change would remove the need of importing File (which is already hard to find!) and wraping every file in File when saving a FileField or ImageField.

comment:8 Changed 5 years ago by Cristiano Coelho

Looks like something as simple as

if not isinstance(content, File):
    content = File(content)

at the method doesn't break anything and would remove the requierement from the docs "Note that the content argument should be an instance of django.core.files.File, not Python’s built-in file object. You can construct a File from an existing Python file object like this:" making it simpler to save a file.

comment:9 Changed 8 months ago by David Smith

Has patch: set
Owner: changed from nobody to David Smith
Status: newassigned

If I have understood this correctly I think this already works.

PR with tests showing conversion to Django's File type.

comment:10 Changed 8 months ago by Mariusz Felisiak

David, this issue is still valid because we don't have a conversion in the Conversion is made in the Storage class, but can be omitted by custom storages. I'm not sure why using Django's File is an issue. I think we should close it unless someone can provide the use case and motivation.

comment:11 Changed 8 months ago by Mariusz Felisiak

Resolution: needsinfo
Status: assignedclosed
Note: See TracTickets for help on using tickets.
Back to Top