我想訪問串行器類中的查詢字符串值。我如何訪問串行器Django Rest框架中的QueryString值
class OneZeroSerializer(rest_serializer.ModelSerializer):
location = rest_serializer.SerializerMethodField('get_alternate_name')
def get_alternate_name(self, obj):
view = self.context['view']
print view.kwargs['q'] #output is {}
return 'foo'
class Meta:
model = OneZero
fields = ('id', 'location')
查看
class OneZeroViewSet(viewsets.ModelViewSet):
serializer_class = OneZeroSerializer
queryset = OneZero.objects.all()
是訪問查詢字符串這個正確的方式?
對於較新的版本:'request.QUERY_PARAMS'已被棄用,自3.0版開始支持'request.query_params',並且從版本3.2開始已經完全刪除。 – lukik