{"id":162,"date":"2026-03-03T09:19:58","date_gmt":"2026-03-03T04:19:58","guid":{"rendered":"https:\/\/gigz.pk\/python\/?post_type=lesson&#038;p=162"},"modified":"2026-03-17T08:52:53","modified_gmt":"2026-03-17T03:52:53","slug":"django-rest-framework","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/python\/lesson\/django-rest-framework\/","title":{"rendered":"Django REST Framework"},"content":{"rendered":"\n<p><strong>Django REST Framework (DRF)<\/strong> is a powerful toolkit built on top of Django for building <strong>Web APIs<\/strong>.<\/p>\n\n\n\n<p>It allows you to create RESTful APIs easily and efficiently using Django models.<\/p>\n\n\n\n<p>If Django is used for building websites, DRF is used for building APIs for:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Mobile applications<\/li>\n\n\n\n<li>Single Page Applications (React, Angular, Vue)<\/li>\n\n\n\n<li>Third-party integrations<\/li>\n\n\n\n<li>Microservices<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Why Use Django REST Framework?<\/h2>\n\n\n\n<p>DRF provides:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Easy serialization of data<\/li>\n\n\n\n<li>Authentication and permissions<\/li>\n\n\n\n<li>Browsable API interface<\/li>\n\n\n\n<li>Built-in support for JSON<\/li>\n\n\n\n<li>Powerful request handling<\/li>\n\n\n\n<li>Viewsets and routers<\/li>\n\n\n\n<li>Token authentication<\/li>\n<\/ul>\n\n\n\n<p>It saves a lot of development time.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Installing Django REST Framework<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">pip install djangorestframework<\/pre>\n\n\n\n<p>Add it to <code>settings.py<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">INSTALLED_APPS = [<br>    ...<br>    'rest_framework',<br>]<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">What is Serialization?<\/h2>\n\n\n\n<p>Serialization converts complex data (like Django models) into JSON format.<\/p>\n\n\n\n<p>Example Model:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from django.db import modelsclass Product(models.Model):<br>    name = models.CharField(max_length=100)<br>    price = models.IntegerField()<\/pre>\n\n\n\n<p>Create a Serializer:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from rest_framework import serializers<br>from .models import Productclass ProductSerializer(serializers.ModelSerializer):<br>    class Meta:<br>        model = Product<br>        fields = '__all__'<\/pre>\n\n\n\n<p>Now the model can be converted to JSON.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating an API View<\/h2>\n\n\n\n<p>Example API view:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from rest_framework.decorators import api_view<br>from rest_framework.response import Response<br>from .models import Product<br>from .serializers import ProductSerializer@api_view(['GET'])<br>def product_list(request):<br>    products = Product.objects.all()<br>    serializer = ProductSerializer(products, many=True)<br>    return Response(serializer.data)<\/pre>\n\n\n\n<p>This returns data in JSON format.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example JSON Response<\/h2>\n\n\n\n<pre class=\"wp-block-preformatted\">[<br>  {<br>    \"id\": 1,<br>    \"name\": \"Laptop\",<br>    \"price\": 80000<br>  },<br>  {<br>    \"id\": 2,<br>    \"name\": \"Mobile\",<br>    \"price\": 30000<br>  }<br>]<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using ViewSets (Cleaner Approach)<\/h2>\n\n\n\n<p>DRF provides ViewSets to reduce repetitive code.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from rest_framework import viewsets<br>from .models import Product<br>from .serializers import ProductSerializerclass ProductViewSet(viewsets.ModelViewSet):<br>    queryset = Product.objects.all()<br>    serializer_class = ProductSerializer<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using Routers<\/h2>\n\n\n\n<p>In <code>urls.py<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from rest_framework.routers import DefaultRouter<br>from .views import ProductViewSetrouter = DefaultRouter()<br>router.register(r'products', ProductViewSet)urlpatterns = router.urls<\/pre>\n\n\n\n<p>Now DRF automatically creates:<\/p>\n\n\n\n<p>GET \/products<br>POST \/products<br>GET \/products\/1<br>PUT \/products\/1<br>DELETE \/products\/1<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Authentication in DRF<\/h2>\n\n\n\n<p>DRF supports:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Session Authentication<\/li>\n\n\n\n<li>Token Authentication<\/li>\n\n\n\n<li>JWT Authentication<\/li>\n<\/ul>\n\n\n\n<p>Example setting:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">REST_FRAMEWORK = {<br>    'DEFAULT_AUTHENTICATION_CLASSES': [<br>        'rest_framework.authentication.SessionAuthentication',<br>        'rest_framework.authentication.TokenAuthentication',<br>    ],<br>}<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Permissions<\/h2>\n\n\n\n<p>You can restrict API access:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from rest_framework.permissions import IsAuthenticated<br>from rest_framework.decorators import api_view, permission_classes@api_view(['GET'])<br>@permission_classes([IsAuthenticated])<br>def secure_view(request):<br>    return Response({\"message\": \"Authenticated User\"})<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Browsable API<\/h2>\n\n\n\n<p>One powerful feature of DRF is its built-in web interface.<\/p>\n\n\n\n<p>When you open the API URL in a browser, you can:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>View data<\/li>\n\n\n\n<li>Send POST requests<\/li>\n\n\n\n<li>Update data<\/li>\n\n\n\n<li>Delete data<\/li>\n<\/ul>\n\n\n\n<p>This is very helpful during development.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Learn Django REST Framework?<\/h2>\n\n\n\n<p>DRF helps you:<\/p>\n\n\n\n<p>Build APIs quickly<br>Connect backend with mobile apps<br>Create scalable backend systems<br>Develop modern web applications<br>Build microservices<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Key Takeaway<\/h2>\n\n\n\n<p>Django REST Framework extends Django to build powerful RESTful APIs.<\/p>\n\n\n\n<p>It simplifies serialization, authentication, permissions, and routing \u2014 making API development faster, cleaner, and more scalable.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/python\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">PYTHON FOR WEB DEVELOPMENT (PYWEB) > REST API Development > Django REST Framework<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773719653422\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":89,"template":"","class_list":["post-162","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Django REST Framework - One Language. Endless Possibilities<\/title>\n<meta name=\"description\" content=\"Learn Django REST Framework: build scalable APIs with serialization, authentication, permissions, and browsable interfaces quickly.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/python\/lesson\/django-rest-framework\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Django REST Framework - One Language. Endless Possibilities\" \/>\n<meta property=\"og:description\" content=\"Learn Django REST Framework: build scalable APIs with serialization, authentication, permissions, and browsable interfaces quickly.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/python\/lesson\/django-rest-framework\/\" \/>\n<meta property=\"og:site_name\" content=\"One Language. Endless Possibilities\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-17T03:52:53+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/django-rest-framework\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/django-rest-framework\\\/\",\"name\":\"Django REST Framework - One Language. Endless Possibilities\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\"},\"datePublished\":\"2026-03-03T04:19:58+00:00\",\"dateModified\":\"2026-03-17T03:52:53+00:00\",\"description\":\"Learn Django REST Framework: build scalable APIs with serialization, authentication, permissions, and browsable interfaces quickly.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/django-rest-framework\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/django-rest-framework\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/lesson\\\/django-rest-framework\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PYTHON FOR WEB DEVELOPMENT (PYWEB) > REST API Development > Django REST Framework\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/python\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/python\\\/\",\"name\":\"One Language. Endless Possibilities\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/python\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Django REST Framework - One Language. Endless Possibilities","description":"Learn Django REST Framework: build scalable APIs with serialization, authentication, permissions, and browsable interfaces quickly.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/python\/lesson\/django-rest-framework\/","og_locale":"en_US","og_type":"article","og_title":"Django REST Framework - One Language. Endless Possibilities","og_description":"Learn Django REST Framework: build scalable APIs with serialization, authentication, permissions, and browsable interfaces quickly.","og_url":"https:\/\/gigz.pk\/python\/lesson\/django-rest-framework\/","og_site_name":"One Language. Endless Possibilities","article_modified_time":"2026-03-17T03:52:53+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/python\/lesson\/django-rest-framework\/","url":"https:\/\/gigz.pk\/python\/lesson\/django-rest-framework\/","name":"Django REST Framework - One Language. Endless Possibilities","isPartOf":{"@id":"https:\/\/gigz.pk\/python\/#website"},"datePublished":"2026-03-03T04:19:58+00:00","dateModified":"2026-03-17T03:52:53+00:00","description":"Learn Django REST Framework: build scalable APIs with serialization, authentication, permissions, and browsable interfaces quickly.","breadcrumb":{"@id":"https:\/\/gigz.pk\/python\/lesson\/django-rest-framework\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/python\/lesson\/django-rest-framework\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/python\/lesson\/django-rest-framework\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/python\/"},{"@type":"ListItem","position":2,"name":"PYTHON FOR WEB DEVELOPMENT (PYWEB) > REST API Development > Django REST Framework"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/python\/#website","url":"https:\/\/gigz.pk\/python\/","name":"One Language. Endless Possibilities","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/python\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson\/162","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/python\/wp-json\/wp\/v2\/media?parent=162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}