0
我想從我的模型獲得的相關圖像URL預期的字符串或緩衝區
class DetailSerializer(serializers.ModelSerializer):
image = serializers.SerializerMethodField("get_related_image_url")
etc
def get_related_image_url(self, obj):
request = self.context.get('request', None)
print(request.build_absolute_uri(obj.image()))
錯誤:
預期字符串或緩衝區
/Users/user/Documents/workspace/demo/django-env/lib/python2.7/site-packages/django/http/request.py in build_absolute_uri
"""
Builds an absolute URI from the location and the variables available in
this request. If no location is specified, the absolute URI is built on
``request.get_full_path()``.
"""
if not location:
location = self.get_full_path()
if not absolute_http_url_re.match(location): ...
current_uri = '%s://%s%s' % ('https' if self.is_secure() else 'http',
self.get_host(), self.path)
location = urljoin(current_uri, location)
return iri_to_uri(location)
def _is_secure(self):
爲什麼我得到這個錯誤?
不知道。什麼是'obj'?什麼'obj.image()'返回? – 2014-09-19 12:34:02
嗨@DanielRoseman所以我應該把在OP將這是一個序列化在DRF – Prometheus 2014-09-19 12:36:49
@DanielRoseman obj.image()返回圖像/ a8206638528a.png從我的模型 – Prometheus 2014-09-19 12:45:18