I’ve been working with Ruby on Rails for a long time, and honestly, it always felt like pure magic.
Rails' elegance, its conventions, the way things just worked with minimal setup. It made development fast and enjoyable.
Recently, I wanted to work with Django (Python’s popular web framework). Especially because of the growing interest in Python and AI.
But when I started, I really missed the convenience and flow that Rails gave me.
So, I started exploring:
How can we get that Rails-like magic in Django too?
Let’s take a look 👇
Serializers — Handling Data Representation
In Rails, we use serializers to control how our model data looks:
# app/serializers/post_serializer.rb
class PostSerializer < ActiveModel::Serializer
attributes :id, :title, :content
end
In Django, it's similar using Django REST Framework:
First, install the djangorestframework
package
pip install djangorestframework
# serializers.py
from rest_framework import serializers
from .models import Post
class PostSerializer(serializers.ModelSerializer):
class Meta:
model = Post
fields = ['id', 'title', 'content']
And how to call those serializers?!.
No worries, we going to see in upcoming.😎
Controllers and Views — Managing Requests
In Rails, we use controllers like this:
# app/controllers/posts_controller.rb
class PostsController < ApplicationController
def index
@posts = Post.all
render json: @posts
end
end
In Django, you can use ViewSets to do the same:
# views.py
from rest_framework import viewsets
from .models import Post
from .serializers import PostSerializer
class PostViewSet(viewsets.ModelViewSet):
queryset = Post.objects.all()
serializer_class = PostSerializer
Auto CRUD — no manual GET, POST, PUT handling! 😃
Routing — Clean and Automatic
In Rails, adding routes is super simple:
# config/routes.rb
resources :posts
In Django, you can achieve the same using DRF’s routers:
# urls.py
from rest_framework.routers import DefaultRouter
from .views import PostViewSet
router = DefaultRouter()
router.register(r'posts', PostViewSet, basename='post')
urlpatterns = router.urls
Resourceful routes with a few lines! ✌️
Callbacks — Hooking into Model Events
In Rails
class Post < ApplicationRecord
before_save :capitalize_title
end
Django (with signals):
from django.db.models.signals import pre_save
from django.dispatch import receiver
@receiver(pre_save, sender=Post)
def capitalize_title(sender, instance, **kwargs):
instance.title = instance.title.capitalize()
✅Behavior before/after saving models — sorted!
Validations — Ensuring Data Integrity
In Rails
class Post < ApplicationRecord
validates :title, presence: true
end
In Django
from django.db import models
class Post(models.Model):
title = models.CharField(max_length=100)
def clean(self):
if not self.title:
raise ValidationError('Title cannot be empty')
Django models come with built-in validation hooks.
(Also, Django’s CharField automatically validates max_length like Rails does!)
Wrapping Up
Even though Rails and Django have different styles, you can recreate almost all of Rails' magic in Django — once you know where to look!
From serializers, viewsets, routers, callbacks, validations, and beyond — Django can feel just as developer-friendly as Rails.
And this is just the beginning!
In upcoming parts, we'll dive deeper into topics like building APIs, authentication, background jobs, and more.
Stay tuned for Part 2! 🚀