Django UpdateModelMixin: Easily Add Update Functionality to Your API

Last updated 4 months ago | 316 views 75     5

Tags:- Python Django DRF

Introduction: Why UpdateModelMixin Matters

When building APIs, updating existing resources is a must-have feature. Whether you're editing a blog post, updating user settings, or changing an order status—you need a way to handle PUT and PATCH requests cleanly and efficiently.

That’s where Django REST Framework’s UpdateModelMixin shines. It provides built-in logic to update model instances using minimal boilerplate, saving you from writing repetitive code.

In this article, we’ll explore how to use UpdateModelMixin to implement update functionality with both full (PUT) and partial (PATCH) support.


What is UpdateModelMixin?

UpdateModelMixin is a DRF mixin that provides an .update() method, allowing you to easily build API views that support updating a single object.

It handles:

  • Retrieving the object via get_object()

  • Validating incoming data with a serializer

  • Updating the model instance

  • Returning a response (200 OK)

You typically use it with GenericAPIView, or pair it with other mixins like RetrieveModelMixin or DestroyModelMixin.


How UpdateModelMixin Works

Internals of the .update() method:

  • Accepts request, *args, and **kwargs

  • Calls get_object() to fetch the object

  • Uses serializer.save() to update the object

  • Returns a 200 response with the updated data


Step-by-Step Implementation of UpdateModelMixin

1. Create a Django Model

# models.py
from django.db import models

class Profile(models.Model):
    name = models.CharField(max_length=100)
    email = models.EmailField()
    bio = models.TextField(blank=True)

    def __str__(self):
        return self.name

2. Build the Serializer

# serializers.py
from rest_framework import serializers
from .models import Profile

class ProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = Profile
        fields = '__all__'

3. Create the Update View Using UpdateModelMixin

# views.py
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import UpdateModelMixin
from .models import Profile
from .serializers import ProfileSerializer

class ProfileUpdateView(UpdateModelMixin, GenericAPIView):
    queryset = Profile.objects.all()
    serializer_class = ProfileSerializer

    # Handles PUT and PATCH requests
    def put(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)

    def patch(self, request, *args, **kwargs):
        return self.partial_update(request, *args, **kwargs)

4. Hook It Up in urls.py

# urls.py
from django.urls import path
from .views import ProfileUpdateView

urlpatterns = [
    path('profiles/<int:pk>/update/', ProfileUpdateView.as_view(), name='profile-update'),
]

✅ Full Functional Example

# models.py
class Profile(models.Model):
    name = models.CharField(max_length=100)
    email = models.EmailField()
    bio = models.TextField(blank=True)
# serializers.py
class ProfileSerializer(serializers.ModelSerializer):
    class Meta:
        model = Profile
        fields = '__all__'
# views.py
class ProfileUpdateView(UpdateModelMixin, GenericAPIView):
    queryset = Profile.objects.all()
    serializer_class = ProfileSerializer

    def put(self, request, *args, **kwargs):
        return self.update(request, *args, **kwargs)

    def patch(self, request, *args, **kwargs):
        return self.partial_update(request, *args, **kwargs)
# urls.py
urlpatterns = [
    path('profiles/<int:pk>/update/', ProfileUpdateView.as_view(), name='profile-update'),
]

Now, you can:

  • Send a PUT request to /profiles/1/update/ to fully update a profile.

  • Send a PATCH request to the same URL to update only specific fields.


Tips & Common Pitfalls

✅ Best Practices

  • Use PATCH for partial updates to avoid overwriting unintended fields.

  • Validate data in your serializer—especially for critical fields like emails or passwords.

  • Log changes or send notifications if your application requires tracking updates.

❌ Common Mistakes

Mistake What Happens Fix
Forgetting .partial=True in PATCH Fields not provided are required unnecessarily Use partial_update() correctly
Not including put() or patch() views DRF throws 405 Method Not Allowed Define both methods in the view
Omitting queryset in view Throws assertion error or fails silently Always define queryset

PUT vs PATCH: Quick Comparison

Feature PUT PATCH
Update Type Full update Partial update
Missing Fields Will be overwritten Will be ignored
Use Case Replace entire resource Update specific fields

When to Use UpdateModelMixin vs UpdateAPIView

Use Case Recommendation
Want flexibility + multiple mixins UpdateModelMixin
Only need update functionality UpdateAPIView
Combining retrieve, update, and delete Use in RetrieveUpdateDestroyAPIView

Conclusion: Simplify Your API with UpdateModelMixin

Django’s UpdateModelMixin helps you implement robust update functionality with minimal effort. It’s flexible, composable, and ideal for situations where you want clean, maintainable views without boilerplate.

To summarize:

  • Use it with GenericAPIView for reusable views

  • Add both put() and patch() for complete and partial updates

  • Combine with other mixins to build powerful, DRY API views