Opened 4 years ago

Closed 4 years ago

#32452 closed New feature (wontfix)

Simplify storing strings/bytes in a Storage

Reported by: Thomas Güttler Owned by: nobody
Component: File uploads/storage Version: 3.1
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Thomas Güttler)

Docs: https://docs.djangoproject.com/en/3.1/topics/files/#storage-objects

path = default_storage.save('path/to/file', ContentFile(b'new content'))

Things would be bit easier, if save() would support strings/bytes directly.

This way a developer needs to type and import less code:

path = default_storage.save('path/to/file', b'new content')
path = default_storage.save('path/to/file', 'new unicode string, encoded automatically with utf-8')

Would you accept a patch to Storage.save()?

Background: I am writing a unittest which mocked away the storage calls to save() will put the data into the mock.

Then I check if the data is the way I want it.

Now it gets complicated again: I need to access the data which is inside ContentFile grrr.

This would be much easier if you could add strings/bytes to the save() directly.

Change History (3)

comment:1 by Thomas Güttler, 4 years ago

Description: modified (diff)

comment:2 by Thomas Güttler, 4 years ago

Description: modified (diff)

comment:3 by Mariusz Felisiak, 4 years ago

Resolution: wontfix
Status: newclosed

Things would be bit easier, if save() would support strings/bytes directly.

IMO it would be confusing. Storage.save() save content to the file, so it supports files and any file-like objects, e.g. StringIO or BytesIO. Making mocking easier is not a sufficient argument for me, it's also not clear why this is much harder with the current API.

You can start a discussion on DevelopersMailingList if you don't agree.

Note: See TracTickets for help on using tickets.
Back to Top