【问题标题】:Expected string or buffer预期的字符串或缓冲区
【发布时间】:2014-09-19 21:18:19
【问题描述】:

我正在尝试从我的模型中获取相关的图像 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() 返回什么?
  • 嗨 @DanielRoseman 所以我应该把 OP 放在 DRF 中,这是一个序列化器
  • @DanielRoseman obj.image() 从我的模型返回 images/a8206638528a.png
  • 除非它不将其作为字符串返回,否则您将不会收到该错误。请显示实际方法的代码。
  • @DanielRoseman 你知道你是对的。它正在重新调整 json !!!!!!

标签: python django django-rest-framework


【解决方案1】:

obj.image() 似乎返回的不是字符串。

【讨论】:

    猜你喜欢
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 2017-12-05
    • 2020-04-02
    相关资源
    最近更新 更多