Binge On Code

Jun 7, 2023

213 readers

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:

  • It must come first in the decorators list for a given method, be they custom or any other DRF decorator.
  • 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.
  • 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!

 

Related Articles

DRF Request - A Gentle Introduction - Part Three

Once a DRF Request has been deemed valid, the next step is returning data appropriately and it is exactly what we discuss in this DRF request tutorial.

Jun 7, 2023

Views 196

What are DRF view decorators and how to use them

DRF view decorators make it possible to eliminate the redundancy in code and ensures that code conforms to best practices in a uniform manner.

Jun 7, 2023

Views 679

DRF Request - A Gentle Introduction - Part One

A DRF Request is what you send to the server to make first or subsequent contact. Think of a request as how you introduce yourself to the server.

Jun 7, 2023

Views 201

Angular JavaScript React CSS Django Python React Native Next JS State Management Django Rest Framework Unity