mirror of
https://github.com/encode/django-rest-framework.git
synced 2025-04-16 07:02:10 +03:00
Disable LoginRequiredMiddleware on ViewSets
This commit is contained in:
parent
f7e8556eec
commit
2458547b96
|
@ -19,6 +19,7 @@ automatically.
|
|||
from functools import update_wrapper
|
||||
from inspect import getmembers
|
||||
|
||||
from django import VERSION as DJANGO_VERSION
|
||||
from django.urls import NoReverseMatch
|
||||
from django.utils.decorators import classonlymethod
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
|
@ -136,6 +137,12 @@ class ViewSetMixin:
|
|||
view.cls = cls
|
||||
view.initkwargs = initkwargs
|
||||
view.actions = actions
|
||||
|
||||
# Exempt from Django's LoginRequiredMiddleware. Users should set
|
||||
# DEFAULT_PERMISSION_CLASSES to 'rest_framework.permissions.IsAuthenticated' instead
|
||||
if DJANGO_VERSION >= (5, 1):
|
||||
view.login_required = False
|
||||
|
||||
return csrf_exempt(view)
|
||||
|
||||
def initialize_request(self, request, *args, **kwargs):
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
import unittest
|
||||
from functools import wraps
|
||||
|
||||
import pytest
|
||||
from django import VERSION as DJANGO_VERSION
|
||||
from django.db import models
|
||||
from django.test import TestCase, override_settings
|
||||
from django.urls import include, path
|
||||
|
@ -196,6 +198,11 @@ class InitializeViewSetsTestCase(TestCase):
|
|||
assert get.view.action == 'list_action'
|
||||
assert head.view.action == 'list_action'
|
||||
|
||||
@unittest.skipUnless(DJANGO_VERSION >= (5, 1), 'Only for Django 5.1+')
|
||||
def test_login_required_middleware_compat(self):
|
||||
view = ActionViewSet.as_view(actions={'get': 'list'})
|
||||
assert view.login_required is False
|
||||
|
||||
|
||||
class GetExtraActionsTests(TestCase):
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user