我如何跳过TokenAuthentication类只获取请求django restframework

时间:2016-05-24 05:55:55

标签: python django django-rest-framework

我想要@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_intent_breh); if (myBundle != null) { String name = myBundle.getString("workout"); ShowDetails(name); } } private void ShowAbDetails(String mName) { if(mName.equals("abs1")){ onBackPressed(); } } @Override public void onBackPressed() { // your logic here } TokenAuthenticationpost课程。但是对于get请求,我不想要这个认证类。我怎样才能做到这一点?

class EmailViewSet(viewsets.ModelViewSet):
    queryset = Email.objects.all()
    authentication_classes = (TokenAuthentication,)
    permission_classes = (IsAuthenticated,)
    serializer_class = EmailSerializer

    def create(self, request, *args, **kwargs):
        return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

这里到底发生的是TokenAuthentication类适用于所有类型的请求。但我希望此课程仅适用于POST请求的GET次请求。

1 个答案:

答案 0 :(得分:1)

我认为您可以覆盖get_authenticators功能:

def get_authenticators(self):
    """
    Instantiates and returns the list of authenticators that this view can use.
    """
    if self.request.method == "POST":
        return [TokenAuthentication()]

    return super(EmailViewSet, self).get_authenticators()

然后,从视图中删除authentication_classes

相关问题