Can't decorate a method with @api_view
#9427
-
So I am trying to build something with django rest framework. and I decided to build the views in a class as methods and then decorate them with AssertionError: The and yes I am building a custom admin site. here is the view class Foo:
@api_view()
def index(self, request):
return Response("Hello world")
def get_urls(self):
return [path(''), self.index, name='index], "rest_admin", "rest_admin" and here is the URLs foo = Foo()
urlpatterns = [
path('admin', foo.get_urls())
] after some investigations. I saw that it passes the request as the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Correct, this decorator only works with function, not class methods. If you really insist, you may try your luck with Django's Something along the lines of: class Foo:
@method_decorator(api_view())
def index(self, request):
return Response("Hello world")
def get_urls(self):
return [path(''), self.index, name='index], "rest_admin", "rest_admin" I have not tested it with |
Beta Was this translation helpful? Give feedback.
Correct, this decorator only works with function, not class methods. If you really insist, you may try your luck with Django's
method_decorator
: https://docs.djangoproject.com/en/5.0/ref/utils/#django.utils.decorators.method_decoratorSomething along the lines of:
I have not tested it with
api_view
, but this worked for me in other similar cases.