简体   繁体   中英

Django how to pass header on request in get_queryset

I am using Django rest framework to pass a get request with a query_param like so:

views.py

class PlanView(generics.ListAPIView):
    """
    API endpoint which allows prices to be viewed or edited
    """

    serializer_class = PlanSerializer
    permission_classes = (IsAuthenticated,)

    # override method
    def get_queryset(self):
        queryset = Plan.objects.all()
        size = self.request.query_params.get("size", None)
        if portion is not None:
            if size == "large":
                queryset = queryset.filter(Large=True)
            elif size == "small":
                queryset = queryset.filter(Small=False)
        return queryset

however, I set up an authentication class in settings.py like this:

'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.TokenAuthentication'
    ]

so that I can pass a header with a token in request. This works well when I test with a third party client like postman. however how can I pass an authorization token as header in the above view?

如果您在DRF中将标头作为size传递,并且要在get_qeryset()方法中访问它,则必须使用self.request.META['HTTP_SIZE']访问它

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM