Skip to content
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

Fix api_view decorator with Django 4 #8291

Merged
merged 1 commit into from
Dec 13, 2021

Conversation

tim-mccurrach
Copy link
Contributor

@tim-mccurrach tim-mccurrach commented Dec 12, 2021

Description

When using Django 4, we get slightly unexpected behaviour using the api_view decorator:

@api_view(["POST"])
def some_view(request):
    pass
# Django 3.2
>>> some_view.__name__
'some_view'
>>> some_view.__qualname__
'WrappedAPIView'
# Django 4.0
>>> some_view.__name__
'view'
>>> some_view.__qualname__
'View.as_view.<locals>.view'

Further Discussion

This change is caused by a change in the way that Django 4 implements View.as_view(). The change and a full discussion can be viewed here, but in short: as_view used to use update_wrapper and now it doesn't.

I was definitely for the change above, and argued against using update_wrapper in that case, but for a decorator such as api_view it is definitely appropriate, and I would expect things like __name__ and __qualname__ to be preserved from the original function.

When using a class-based view you have written, using view_class seems a reasonable approach. But in this case, the fact you have a class-based view is obscured unless you read the source code and so I think __name__ should be copied over.

@carltongibson carltongibson mentioned this pull request Dec 13, 2021
@adamchainz
Copy link
Contributor

Seems reasonable to me.

@tomchristie tomchristie merged commit 9c97946 into encode:master Dec 13, 2021
tomchristie added a commit that referenced this pull request Dec 15, 2021
sigvef pushed a commit to sigvef/django-rest-framework that referenced this pull request Dec 3, 2022
sigvef pushed a commit to sigvef/django-rest-framework that referenced this pull request Dec 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants