Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
145 views

Introduction To Django REST Framework

This document provides an introduction to Django REST Framework. It covers setting up REST Framework, creating API views using functions and classes, serializers for converting data, generic class-based views, view sets, and testing APIs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
145 views

Introduction To Django REST Framework

This document provides an introduction to Django REST Framework. It covers setting up REST Framework, creating API views using functions and classes, serializers for converting data, generic class-based views, view sets, and testing APIs.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Intro to Django

REST Framework

| Pablo Trinidad
Inventive
Major League Hacking

Pablo Trinidad

| Pablo Trinidad
API
A pplication

Programming

I
nterface
REST
RE presentional

State

T
ransfer
Set up!

$ pip install django-rest-framework


Set up!

INSTALLED_APPS = (

rest_framework,
)
Views

from rest_framework.decorators import api_view


from rest_framework.response import Response

@api_view()
def hello_world(request):
return Response({message: Hello, world!})
Views
Methods

@api_view([GET, POST)
def hello_world(request):
return Response({
message: Hello, world!
})
Class-based Views

from rest_framework.views import APIView


from rest_framework.response import Response

class UsersView(APIView):

def get(self, request):


# Magic

def post(self, request):


# Magic
Serializers

from rest_framework import serializers

class UserSerializer(serializers.ModelSerializer):

class Meta:
model = User
fields = (id,name,email)
Generic Class-based Views

CreateAPIView
ListAPIView
RetreiveAPIView
DestroyAPIView
UpdateAPIView
ListCreateView
RetrieveUpdateView
RetireveDestroyView
RetreiveUpdateDestroyView
ViewSets

from rest_framework.viewsets import ModelViewSet

class UserViewSet(ModelViewSet):

queryset = User.objects.all()
serializer_class = UserSerializer
Tests

from rest_framework.test import APITestCase

class UserTests(APITestCase):

def testList(self):
request = self.client.post(/api/)

You might also like