How to make tastypie give the url of a file instead of its path?

蓝咒 提交于 2019-12-11 05:09:10

问题


I'm using Django and Tastypie to create a RESTful web app. I have a model like this

class Picture(models.Model):
    # some other fields
    image = models.ImageField('Image')
    def image_url(self):
        return self.image.url

Tastypie will give the image's path but actually I need its url. How to write a correct resource api to achive this?


回答1:


By default TastyPie is supposed to return URL of ImageField and FileField. See here: https://github.com/toastdriven/django-tastypie/blob/master/tastypie/fields.py#L185

# 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)

Please make sure that you are using the latest version of TastyPie.



来源:https://stackoverflow.com/questions/10153355/how-to-make-tastypie-give-the-url-of-a-file-instead-of-its-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!