Feature request: Extend Storage.save() to directly save strings/bytes

75 views
Skip to first unread message

guettli

unread,
Feb 18, 2021, 2:37:28 AM2/18/21
to Django developers (Contributions to Django itself)

To save strings/bytes you need to use ContentFile up to now:

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

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

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

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

The implementation would be simple: Storage.save() could check with "isinstance()"

if the value needs to be wrapped in ContentFile.

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.

Adam Johnson

unread,
Feb 18, 2021, 4:04:01 AM2/18/21
to django-d...@googlegroups.com
Hi

If you're mocking away the storage class to speed up your tests, instead try this in-memory storage class which implements the complete API:  https://github.com/waveaccounting/dj-inmemorystorage . Then you don't need any mocks.

As to adding the shortcut - if the only thing it will help with is your test length, why not write a couple of wrapper functions in your tests? I'm not sure we'd want to add this capability to Django since it would complicate the type signature and would enforce change on all the third party storage classes that exist.

Thanks,

Adam

--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/fabd4661-93b9-45f4-a47e-3ffc31abc50cn%40googlegroups.com.


--
Adam

Mariusz Felisiak

unread,
Feb 18, 2021, 6:57:51 AM2/18/21
to Django developers (Contributions to Django itself)

guettli

unread,
Feb 19, 2021, 3:57:24 AM2/19/21
to Django developers (Contributions to Django itself)
Adam Johnson schrieb am Donnerstag, 18. Februar 2021 um 10:04:01 UTC+1:
Hi

If you're mocking away the storage class to speed up your tests, instead try this in-memory storage class which implements the complete API:  https://github.com/waveaccounting/dj-inmemorystorage . Then you don't need any mocks.


Thank you for this hint.
 
As to adding the shortcut - if the only thing it will help with is your test length, why not write a couple of wrapper functions in your tests?

It is about easier usage for storing strings/bytes and easier testing  (if you store strings/bytes).

Of course I can write a wrapper, but I have several django projects. I could create a library which exists of one method with one line.
But why not make life easier for all people?
 
I'm not sure we'd want to add this capability to Django since it would complicate the type signature and would enforce change on all the third party storage classes that exist.

I don't only few Storage Implementation override save(), they override _save():



 
Reply all
Reply to author
Forward
0 new messages