【问题标题】:get the request of an authorized client in the API side?在 API 端获取授权客户端的请求?
【发布时间】:2016-06-05 03:32:46
【问题描述】:

我需要从授权客户端获取响应,客户端有一个令牌可以从 API 请求书籍,所以我的代码如下:

class BooksViewSet(viewsets.ModelViewSet):

    permission_classes = [TokenHasScope]
    required_scopes = ['books']

这里我需要知道哪些书被请求做一个查询并返回一个响应给客户端,我该如何弄清楚?

【问题讨论】:

    标签: django python-2.7 api django-rest-framework


    【解决方案1】:

    你可以像这样覆盖get_queryset

    class BooksViewSet(viewsets.ModelViewSet):
    
        permission_classes = [TokenHasScope]
        required_scopes = ['books']
    
        def get_queryset(self):
            # Replace this queryset with the books you want to query for.
            # You can use self.request to access parameters.
            return Book.objects.all()
    

    【讨论】:

    • 谢谢! “self.request”是我需要使用genericAPIView booktitle = float(self.request.query_params.get('booktitle'))
    猜你喜欢
    • 2015-02-01
    • 2015-07-06
    • 2017-02-22
    • 2014-11-06
    • 2020-01-24
    • 2015-10-15
    • 1970-01-01
    • 2020-09-23
    • 1970-01-01
    相关资源
    最近更新 更多