Get Method in Django REST Framework - Django Tutorial
Get Method in Django REST Framework - Django Tutorial
STUDYGYAAN 11K
Django REST framework (DRF) is a powerful and flexible toolkit for building web APIs. In this
tutorial, we’ll learn how to build a CRUD API in just 15 minutes using the Django REST
framework.
To build our sample to-do list application, we’ll start by setting up the Django REST
framework in a Django project, followed by a complete tutorial on how to create a CRUD
REST API with the Django REST framework.
Then, cd into the new drinks folder and create a new app for your API
Next, add rest_framework and drinks to the INSTALLED_APPS inside the settings.py file:
# settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
https://studygyaan.com/django/get-method-in-django-rest-framework 1/6
13-05-2023 21:15 Get Method in Django REST Framework - Django Tutorial
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'core'
]
class Drink(models.Model):
name=models.CharField(max_length=200)
description = models.CharField(max_length=500)
def __str__(self):
return self.name+" "+self.description
Model serializer
To convert the Model object to an API-appropriate format like JSON, Django REST framework
uses the ModelSerializer class to convert any model to serialized JSON objects:
class DrinkSerializer(serializers.ModelSerializer):
class Meta:
model= Drink
fields = '__all__'
https://studygyaan.com/django/get-method-in-django-rest-framework 2/6
13-05-2023 21:15 Get Method in Django REST Framework - Django Tutorial
The first API view class deals with the core/api/ endpoint, in which it handles GET for listing all
to-dos of a given requested user and POST for creating a new to-do. Notice that we’ve
added permission_classes, which allows authenticated users only:
The GET() method first fetches all the objects from the model by filtering with the requested
user ID. Then, it serializes from the model object to a JSON serialized object. Next, it returns
the response with serialized data and status as 200_OK.
class drink_list(APIView):
"""
List all snippets, or create a new snippet.
"""
def get(self, request, format=None):
drinks = Drink.objects.all()
serializer = DrinkSerializer(drinks, many=True)
return Response(serializer.data)
Detail view
Now that we’ve successfully created our first endpoint view, let’s create the second
endpoint /<int:todo_id> API view.
In this API view class, we need to create three methods for handling the corresponding HTTP
method, GET as discussed above:
https://studygyaan.com/django/get-method-in-django-rest-framework 3/6
13-05-2023 21:15 Get Method in Django REST Framework - Django Tutorial
class drink_list(APIView):
"""
List all snippets, or create a new snippet.
"""
def get(self, request, format=None):
drinks = Drink.objects.all()
serializer = DrinkSerializer(drinks, many=True)
return Response(serializer.data)
class drink_detail(APIView):
Create urls.py file app core and register in main urls.py file
urls.py – main
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('core.urls')),
]
core/urls.py
urlpatterns = [
path('',views.drink_list.as_view()),
path('<int:id>/',views.drink_detail.as_view()),
]
https://studygyaan.com/django/get-method-in-django-rest-framework 4/6
13-05-2023 21:15 Get Method in Django REST Framework - Django Tutorial
Now, we’re ready for the first test. Navigate to http://127.0.0.1:8000. Make sure you’re
logged in with your superuser credentials:
and it will provide the output containing 3 different types of objects that the information we
are entered through admin panel
Now, if you navigate to http://127.0.0.1:8000/<id>/, it will show the detail API view page.
Notice that you correctly navigate to a valid ID. In the screenshot below, I used 1 as the ID:
Conclusion
Congratulations! You’ve successfully built your first fully functional CRUD Django REST API.
https://studygyaan.com/django/get-method-in-django-rest-framework 5/6
13-05-2023 21:15 Get Method in Django REST Framework - Django Tutorial
Building a RESTful API can be complicated, but Django REST framework handles complexity
fairly well. I hope you have fun building new APIs using the Django REST framework, and be
sure to leave a comment if you have any questions. Happy coding!
Tags: django, Django Tutorial, Python Tutorial Share:
© 2022 STUDYGYAAN
https://studygyaan.com/django/get-method-in-django-rest-framework 6/6