Django Rest Framework - 查询带有特殊字符的参数

时间:2018-02-28 13:48:29

标签: django django-rest-framework

我的模特是:

class Skill(models.Model):
    skill = models.CharField()

我的观点:

class SkillViewset(viewsets.ModelViewSet):
    queryset = Skill.objects.all()
    serializer_class = SkillSerializer

    filter_backends = (filters.SearchFilter, filters.OrderingFilter )
    search_fields = ('id', 'skill')
    ordering_fields = '__all__'

    def get_queryset(self):
        queryset = property_models.Skill.objects.all()
        skill = self.request.query_params.get('skill', None)
        if skill is not None:
            queryset = queryset.filter(skill__icontains=skill)
        return queryset

我的一种语言是" C ++"

这有效:

Skill.objects.filter(skill__icontains='C++')

然而,调用我的api不起作用:

/skills/?skill=c++

1 个答案:

答案 0 :(得分:3)

这是因为您必须对参数进行网址编码,以便c++成为c%2B%2B https://www.w3schools.com/tags/ref_urlencode.asp