Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Fix api_view decorator with Django 4 #8291

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 4 additions & 16 deletions 20 rest_framework/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
methods on viewsets that should be included by routers.
"""
import types
from functools import update_wrapper

from django.forms.utils import pretty_name

Expand All @@ -22,18 +23,8 @@ def api_view(http_method_names=None):

def decorator(func):

WrappedAPIView = type(
'WrappedAPIView',
(APIView,),
{'__doc__': func.__doc__}
)

# Note, the above allows us to set the docstring.
# It is the equivalent of:
#
# class WrappedAPIView(APIView):
# pass
# WrappedAPIView.__doc__ = func.doc <--- Not possible to do this
class WrappedAPIView(APIView):
pass

# api_view applied without (method_names)
assert not(isinstance(http_method_names, types.FunctionType)), \
Expand All @@ -52,9 +43,6 @@ def handler(self, *args, **kwargs):
for method in http_method_names:
setattr(WrappedAPIView, method.lower(), handler)

WrappedAPIView.__name__ = func.__name__
WrappedAPIView.__module__ = func.__module__

WrappedAPIView.renderer_classes = getattr(func, 'renderer_classes',
APIView.renderer_classes)

Expand All @@ -73,7 +61,7 @@ def handler(self, *args, **kwargs):
WrappedAPIView.schema = getattr(func, 'schema',
APIView.schema)

return WrappedAPIView.as_view()
return update_wrapper(WrappedAPIView.as_view(), func)

return decorator

Expand Down
10 changes: 10 additions & 0 deletions 10 tests/test_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,16 @@ def view(request):

assert isinstance(view.cls.schema, CustomSchema)

def test_wrapper_assignments(self):
@api_view(["GET"])
def test_view(request):
"""example docstring"""
pass

assert test_view.__name__ == "test_view"
assert test_view.__doc__ == "example docstring"
assert test_view.__qualname__ == "DecoratorTestCase.test_wrapper_assignments.<locals>.test_view"


class ActionDecoratorTestCase(TestCase):

Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.