What is DRF api_view and how to use @api_view?

Binge On Code
3 min readSep 14, 2021
What is DRF api_view and how to use @api_view?

DRF api_view is a simple decorator which makes it possible for you to create function based views in DRF instead of going with class based views.

Well, let’s face it, at times some of the technical jargon you may hear out there or with your peers such as DRF api_view may scare you away. Well, you are in luck. This simple piece will give you a guide into what DRF @api_view really is.

This is simply a decorator! Nothing more. Yeah, DRF api_view decorator makes it possible to augment additional functionality onto a function, making it more capable of doing other functionality which it would otherwise not be able to on it’s own. However, if it were able to, then it would take time to implement. Basically, any DRF decorator will help you augment your functions.

Of course, there is the substitute option of going with class based option, and in that case you would opt to use ApiView instead, Class Based Views.

What does it really add onto functions?

A DRF @api_view decorator will add onto a function ability to extend what ApiView for classes offers. For example, it will convert the received HTTPRequest into a DRF Request object. Along these lines, it will convert the return response into a DRF Response object instead of HTTPResponse.

How to define it.

All you need to do is simply add it as a decorator to your function. For example:

from rest_framework.decorators import api_view


@api_view()
def your_cool_function(request):
"""
Function definition
"""

Just like any other DRF decorator, this decorator will take a single argument, which is a list of HTTP methods it should accept.

By default, DRF @api_view accepts GET HTTP method. So for it to accept other methods, just add them into the list of arguments like so:

from rest_framework.decorators import api_view


@api_view(['GET', 'POST'])
def your_cool_function(request):
"""
Your cool function that now supports GET and POST
HTTP methods.
"""

Important know-how.

Well, now that you know what a DRF api_view is and how to define it, it is IMPORTANT to take note of these pointers:

  • 1. It must come first in the decorators list for a given method, be they custom or any other DRF decorator.
  • 2. It will use the default settings specified in settings file under REST_FRAMEWORK dictionary. Such include authentication, renderers and parsers to mention but a few.
  • 3. By default it allows GET method only. To allow other HTTP methods, add these as a list of arguments. Note that you also need to include the GET method into this list if you still want to support it.

Conclusion.

As you can see, it is easy to define and use DRF api_view decorator.

Happy coding!

Are you interested in learning Django Rest Framework? Checkout similar awesome articles on my website: Binge On Code > Django Rest Framework

--

--

Binge On Code

You love programming? Well, we do! We are a team of one as of now and are really passionate about programming and making the world better today than yesterday!