django-tastypie / django-tastypie

Creating delicious APIs for Django apps since 2010.
http://tastypieapi.org/
Other
3.92k stars 1.17k forks source link

Error saving FileField value with remote storage #1177

Open manuelzs opened 10 years ago

manuelzs commented 10 years ago

On saving a resource with a file field, tastypie overwrites the filename with whatever the url for that file was when using external storage (i.e. django-storages).

The tastypie.fields.FileField dehydrate gets the field url after is calls convert(). For example, a filed with file.txt would return https://example.com/file.txt?token=ABC.

def convert(self, value):
    ...
    try:
        # Try to return the URL if it's a ``File``, falling back to the string
        # itself if it's been overridden or is a default.
        return getattr(value, 'url', value)
    ...

This is what you would expect on a GET. But when the file is saved (PUT, PATCH), tastypie will save that URL (https://example.com/file.txt?token=ABC) to the field instead of the original value (file.txt) corrupting the data. Note that storing the URL is not an option because with services like S3 the URL has to be signed each time.

pcompassion commented 10 years ago

See if this helps.. I got burned by it as well. https://github.com/toastdriven/django-tastypie/issues/1018

manuelzs commented 10 years ago

Thanks @pcompassion, I had already found a temporary solution but I'll have a look at this one.