In this tutorial, the goal is to set up Django REST Framework, a powerful and flexible toolkit for building Web APIs. You'll learn how to install Django, start a new project, and install and configure Django REST Framework.
What will you learn?
Prerequisites:
It's recommended to have a basic understanding of Python programming and some familiarity with web development concepts.
First, we need to install Django and Django REST Framework. If you have Python installed, you can use pip (Python's package manager) to install these packages.
pip install django
pip install djangorestframework
To start a new Django project, use the Django-admin command-line tool. Navigate to the directory where you want your project to live, then run the following command:
django-admin startproject myproject
Replace "myproject" with the name you want to give your project.
To use Django REST Framework, you'll need to add it to your installed apps in your settings.py file which is inside your Django project.
Open settings.py and locate the INSTALLED_APPS section and add 'rest_framework' to the list.
INSTALLED_APPS = [
# ...
'rest_framework',
]
Let's create a simple API. First, we'll need a Django app. You can create one using the following command:
python manage.py startapp api
In the models.py
file of your new app, add the following code:
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=100)
age = models.IntegerField()
profession = models.CharField(max_length=100)
This code creates a Person
model with fields for name, age, and profession.
Next, we will create a serializer in serializers.py
:
from rest_framework import serializers
from .models import Person
class PersonSerializer(serializers.ModelSerializer):
class Meta:
model = Person
fields = ['id', 'name', 'age', 'profession']
The serializer allows complex data such as querysets and model instances to be converted to Python native datatypes that can then be easily rendered into JSON.
Now, let's create a view in views.py
:
from rest_framework import viewsets
from .models import Person
from .serializers import PersonSerializer
class PersonViewSet(viewsets.ModelViewSet):
queryset = Person.objects.all()
serializer_class = PersonSerializer
The ModelViewSet
class is a type of ViewSet that provides default create()
, retrieve()
, update()
, partial_update()
, destroy()
and list()
actions.
Finally, we need to set up our URL routing in urls.py
:
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import PersonViewSet
router = DefaultRouter()
router.register(r'persons', PersonViewSet)
urlpatterns = [
path('', include(router.urls)),
]
You've learned how to install Django and Django REST Framework and how to start a new Django project. You've also learned how to set up Django REST Framework and create a simple API.
Next steps include learning more about the Django REST Framework, such as how to work with serializers and viewsets in more detail, how to handle authentication and permissions, and how to test your APIs.
Some additional resources include the official Django REST Framework tutorial and the Django REST Framework documentation.
Solution: This will depend on the model you choose to create. For example, if you create a Job
model, your model might look like this:
python
class Job(models.Model):
title = models.CharField(max_length=100)
description = models.TextField()
Your serializer might look like this:
python
class JobSerializer(serializers.ModelSerializer):
class Meta:
model = Job
fields = ['id', 'title', 'description']
And your view might look like this:
python
class JobViewSet(viewsets.ModelViewSet):
queryset = Job.objects.all()
serializer_class = JobSerializer
Solution: You can do this by changing ModelViewSet
to ReadOnlyModelViewSet
:
python
class PersonViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Person.objects.all()
serializer_class = PersonSerializer
Solution: You can do this by adding a SearchFilter
to the viewset:
```python
from rest_framework import filters
class PersonViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Person.objects.all()
serializer_class = PersonSerializer
filter_backends = [filters.SearchFilter]
search_fields = ['name']
```
For further practice, consider building a more complex API with multiple related models and endpoints, or exploring more advanced features of the Django REST Framework, such as pagination and throttling.