Skip to content

Use URL output representation for FileField, not path representation. #1714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from

Conversation

warchildmd
Copy link

Closes #1368.

The additional settings:

# rest framework configuration
REST_FRAMEWORK = {
    ...
    'PREPEND_MEDIA_URL': True
}

@tomchristie tomchristie changed the title Added option to prepend MEDIA_ROOT to FileField fields. Use URL output representation for FileField, not path representation. Aug 18, 2014
@tomchristie
Copy link
Member

We shouldn't need to include MEDIA_URL in the REST_FRAMEWORK settings dictionary, as it is already available as a regular Django setting.

@pySilver
Copy link

Well I'm just using this version of a field and this works just fine:

class FileField(fields.FileField):
    def to_native(self, value):
        """
        Returns full url to a media file
        """
        try:
            return value.url
        except AttributeError:
            pass

        return super(FileField, self).to_native(value)

note you stil need to retrurn f.name since not saved files might not have url yet. And yes – one should use Storage for working with files.

@tomchristie
Copy link
Member

@tomchristie tomchristie closed this Nov 3, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FileField representations should return URLs, not paths.
4 participants