fields.py 文件源码

python
阅读 27 收藏 0 点赞 0 评论 0

项目:django-simple-api 作者: Myagi 项目源码 文件源码
def to_internal_value(self, data):
        """ TODO - I've had to copy this method from HyperlinkedRelatedField purely
        so that it can deal with polymorphic models. This probably is not ideal.
        """
        try:
            http_prefix = data.startswith(('http:', 'https:'))
        except AttributeError:
            self.fail('incorrect_type', data_type=type(data).__name__)

        if http_prefix:
            # If needed convert absolute URLs to relative path
            data = urlparse.urlparse(data).path
            prefix = get_script_prefix()
            if data.startswith(prefix):
                data = '/' + data[len(prefix):]

        try:
            match = resolve(data)
        except Resolver404:
            self.fail('no_match')

        if self._serializer_is_polymorphic:
            # TODO - This is not really what we want. Need to make sure
            # that match.view_name points to a view which uses a subtype
            # serializer for this polymorphic serializer
            self.view_name = match.view_name

        if match.view_name != self.view_name:
            self.fail('incorrect_match')

        try:
            return self.get_object(match.view_name, match.args, match.kwargs)
        except (ObjectDoesNotExist, TypeError, ValueError):
            self.fail('does_not_exist')
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号