DEV Community

elite_developer
elite_developer

Posted on

How To Integrate Cloudinary In a DRF project.

Integrating Cloudinary with Django REST Framework (DRF) allows you to effortlessly offload asset hosting, keeping your backend stateless and lightweight.

Although there are two main ways to handle this integration:

  1. The Django Storage Way
  2. The Cloudinary Model Field Way

We will learn about the Django storage way today.

In the Django Storage Way, You keep using standard Django ImageField or FileField in your models, and django-cloudinary-storage handles uploading to Cloudinary under the hood. DRF handles serialization seamlessly.
This approach is highly scalable and completely transparent. Your models use native Django fields, meaning if you ever change storage providers (e.g., to AWS S3), you won't have to touch your model schemas or serializers.

1. Install Dependencies
Run the following command in your virtual environment:

pip install django-cloudinary-storage cloudinary pillow
Enter fullscreen mode Exit fullscreen mode

2. Configure settings.py
Add cloudinaryand cloudinary_storageto your INSTALLED_APPS. Note: cloudinary_storage must be placed before django.contrib.staticfiles most preferably at the top.

INSTALLED_APPS = [
    # ...
    'cloudinary_storage',
    'django.contrib.staticfiles',
    'cloudinary',
    'rest_framework',
    'your_app_name',  # Your application
]

# Cloudinary Configuration
CLOUDINARY_STORAGE = {
    'CLOUD_NAME': 'your_cloud_name',
    'API_KEY': 'your_api_key',
    'API_SECRET': 'your_api_secret',
}

# Tell Django to use Cloudinary for Media files
STORAGES = {
    "default": {
        "BACKEND": "cloudinary_storage.storage.MediaCloudinaryStorage",
    },
    "staticfiles": {
        "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage",
    },
}


Enter fullscreen mode Exit fullscreen mode

3. Create the Model
Because Cloudinary is acting as the default storage engine, you just use a normal models.ImageField.

# your_app/models.py
from django.db import models

class Product(models.Model):
    name = models.CharField(max_length=255)
    image = models.ImageField(upload_to='products/')

    def __str__(self):
        return self.name
Enter fullscreen mode Exit fullscreen mode

4. Create the Serializer and View
DRF handles this natively. When returning a response, DRF's ImageField will automatically output the absolute secure URL pointing to Cloudinary.

# your_app/serializers.py
from rest_framework import serializers
from .models import Product

class ProductSerializer(serializers.ModelSerializer):
    class Meta:
        model = Product
        fields = ['id', 'name', 'image']
Enter fullscreen mode Exit fullscreen mode
# your_app/views.py
from rest_framework.viewsets import ModelViewSet
from rest_framework.parsers import MultiPartParser, FormParser
from .models import Product
from .serializers import ProductSerializer

class ProductViewSet(ModelViewSet):
    queryset = Product.objects.all()
    serializer_class = ProductSerializer
    # FormParser and MultiPartParser are required to handle file uploads
    parser_classes = [MultiPartParser, FormParser]
Enter fullscreen mode Exit fullscreen mode

Viola! You've successfully integrated Cloudinary in your DRF Project!

Top comments (0)