Creating a Basic Update API with Django REST Framework
Last updated 2 weeks, 6 days ago | 90 views 75 5

An Update API allows clients to modify an existing resource in the database. In RESTful APIs, this usually corresponds to a PUT
or PATCH
request.
In this guide, we’ll walk through how to create an API endpoint to update a book’s details using Django REST Framework.
What is an Update API?
An Update API modifies data for a single object in the database:
-
PUT: Replaces the entire object
-
PATCH: Partially updates the object
Example:
PUT /books/1/
Request body:
{
"title": "Updated Title",
"author": "Updated Author"
}
Step-by-Step: Create an Update API
We’ll assume you already have a Book
model and are using Django REST Framework.
1. Ensure You Have the Book Model
# books/models.py
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)
def __str__(self):
return self.title
Migrate if needed:
python manage.py makemigrations
python manage.py migrate
2. Create the Serializer
# books/serializers.py
from rest_framework import serializers
from .models import Book
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = '__all__'
3. Create the UpdateAPIView
# books/views.py
from rest_framework.generics import UpdateAPIView
from .models import Book
from .serializers import BookSerializer
class BookUpdateView(UpdateAPIView):
queryset = Book.objects.all()
serializer_class = BookSerializer
✅ UpdateAPIView
handles both PUT
and PATCH
requests for updating a model instance.
4. Add the URL Route
# books/urls.py
from django.urls import path
from .views import BookUpdateView
urlpatterns = [
path('books/<int:pk>/update/', BookUpdateView.as_view(), name='book-update'),
]
Include it in the main project URL:
# myapi/urls.py
from django.urls import path, include
urlpatterns = [
path('api/', include('books.urls')),
]
✅ Testing the Update API
Start the server:
python manage.py runserver
Test via curl, Postman, or DRF web interface:
PUT Request – full update:
PUT http://127.0.0.1:8000/api/books/1/update/
Body:
{
"title": "New Title",
"author": "New Author"
}
PATCH Request – partial update:
PATCH http://127.0.0.1:8000/api/books/1/update/
Body:
{
"title": "Only Title Changed"
}
Response:
{
"id": 1,
"title": "Only Title Changed",
"author": "New Author"
}
✅ Summary
Feature | Description |
---|---|
UpdateAPIView |
Used to update existing model instances |
PUT |
Replaces all fields of a resource |
PATCH |
Updates only the specified fields |
BookSerializer |
Serializes/deserializes request data |
<int:pk> |
Identifies which object to update |
⚠️ Common Pitfalls
Problem | Solution |
---|---|
404 Not Found | Ensure the object with that ID exists |
400 Bad Request | Double-check that all required fields are present for PUT |
ReadOnlyField error | Ensure you don’t include fields that are read-only in updates |
Tips
-
Use PATCH when updating only a few fields to reduce payload size.
-
Customize validation with
validate_<field>
orvalidate()
in the serializer. -
Use Django admin to create initial objects for testing.
-
Combine multiple views using
ModelViewSet
for cleaner architecture.
What’s Next?
Now that you've built a working Update API, consider implementing:
-
✅ Delete API to remove resources
-
✅ Combine List, Retrieve, Create, Update, and Delete into one
ModelViewSet
-
Add permissions and authentication
-
Add unit tests for your views