如何使好吃的人给一个文件的网址,而不是它的路径? [英] How to make tastypie give the url of a file instead of its path?

查看:54
本文介绍了如何使好吃的人给一个文件的网址,而不是它的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django和Tastypie创建RESTful Web应用程序.我有这样的模型

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会给出图像的路径,但实际上我需要它的URL.如何编写正确的资源api来实现这一目标?

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

推荐答案

默认情况下,TastyPie应该返回ImageField和FileField的URL.参见此处: https://github.com/toastdriven/django-tastypie/blob/master/tastypie/fields.py#L185

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)

请确保您使用的是最新版本的DeliciousPie.

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

这篇关于如何使好吃的人给一个文件的网址,而不是它的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆