Setting up a DRF project

Prerequisites

Before you start, ensure you have the following installed:

  • Python (version 3.6 or higher)
  • Django (version 3.0 or higher)
  • pip (Python package installer)

Step 1: Create a Virtual Environment

It’s a good practice to create a virtual environment for your project to manage dependencies. Run the following commands to create and activate a virtual environment:

# Create a virtual environment
python -m venv drf_env

# Activate the virtual environment
# On Windows
drf_env\Scripts\activate

# On macOS/Linux
source drf_env/bin/activate

Step 2: Install Django and Django Rest Framework

With the virtual environment activated, install Django and DRF using pip:

pip install django djangorestframework

Step 3: Create a New Django Project

Create a new Django project by running the following command:

django-admin startproject myproject

Navigate into your project directory:

cd myproject

Step 4: Create a Django App

Create a new app within your project. This app will contain your API logic:

python manage.py startapp myapp

Step 5: Configure Django Settings

Add rest_framework and your new app myapp to the INSTALLED_APPS section in your settings.py file:

# myproject/settings.py

INSTALLED_APPS = [
    ...
    'rest_framework',
    'myapp',
    ...
]

Step 6: Define a Model

In the models.py file of your app, define a simple model. For this example, we’ll create a Book model:

# myapp/models.py

from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.CharField(max_length=100)
    published_date = models.DateField()

    def __str__(self):
        return self.title

Run the following commands to create and apply the migrations for your new model:

python manage.py makemigrations
python manage.py migrate

Step 7: Create a Serializer

Create a serializer for your Book model in a new file serializers.py:

# myapp/serializers.py

from rest_framework import serializers
from .models import Book

class BookSerializer(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = ['id', 'title', 'author', 'published_date']

Step 8: Create a View

Create a view to handle API requests in views.py:

# myapp/views.py

from rest_framework import generics
from .models import Book
from .serializers import BookSerializer

class BookListCreate(generics.ListCreateAPIView):
    queryset = Book.objects.all()
    serializer_class = BookSerializer

Step 9: Configure URLs

Configure the URLs for your API. Create a urls.py file in your app directory and set up the routing:

# myapp/urls.py

from django.urls import path
from .views import BookListCreate

urlpatterns = [
    path('books/', BookListCreate.as_view(), name='book-list-create'),
]

Include the app’s URLs in the project’s main urls.py file:

# myproject/urls.py

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include('myapp.urls')),
]

Step 10: Run the Development Server

Run the Django development server to test your setup:

python manage.py runserver

Open your web browser and navigate to http://127.0.0.1:8000/api/books/. You should see an empty list of books, indicating that your DRF project is set up correctly.

Conclusion

Congratulations! You’ve successfully set up a Django Rest Framework project. You now have a basic API endpoint for managing Book objects. In future tutorials, we will explore more advanced features of DRF, such as authentication, permissions, and more.


Tags: Setting up Django Rest Framework, DRF project setup, Django API tutorial, Django Rest Framework guide