Django
2 min read
Updated 11 Aug 2026
Django and DRF Cheat Sheet
Quick reference guide for common Django CLI commands, ORM queries, and DRF setups.
🧑🏫 Sabse pehle — simple mein samjho#
Ye ek quick reference page hai tumhare daily Django aur DRF tasks ke liye. Ise bookmark karke rakho!
1. Essential CLI Commands#
# Project Setup
django-admin startproject myproject .
python manage.py startapp myapp
# Database & Migrations
python manage.py makemigrations # Detect changes in models
python manage.py migrate # Apply changes to database
# Admin & Server
python manage.py createsuperuser
python manage.py runserver # Start local dev server (port 8000)
python manage.py shell # Open interactive Python shell loaded with Django context
2. ORM Cheat Sheet#
from .models import Post
# Fetching Data
Post.objects.all() # All posts
Post.objects.get(id=1) # Single post (Throws Error if not exactly 1)
Post.objects.filter(title="A") # Many posts matching criteria
Post.objects.exclude(title="A") # Many posts NOT matching criteria
# Field Lookups (Double Underscore)
Post.objects.filter(title__icontains="django") # Case-insensitive contains
Post.objects.filter(created_at__year=2024)
Post.objects.filter(author__username="tarun") # Spanning relationships
# Modifying Data
Post.objects.create(title="A", content="B") # Create and save instantly
post = Post.objects.first()
post.title = "New"
post.save() # Update
post.delete() # Delete
3. URLs and Routing#
from django.urls import path, include
urlpatterns = [
# FBV or CBV
path('about/', views.about_view, name='about'),
path('articles/', views.ArticleListView.as_view(), name='article-list'),
# Path parameters (<type:name>)
path('articles/<int:pk>/', views.ArticleDetail.as_view()),
path('users/<str:username>/', views.user_profile),
# Including other apps
path('api/', include('myapp.urls')),
]
4. Basic DRF ModelViewSet & Router#
If you want to build a complete CRUD API for a model in 2 minutes:
serializers.py
from rest_framework.serializers import ModelSerializer
from .models import Post
class PostSerializer(ModelSerializer):
class Meta:
model = Post
fields = '__all__'
views.py
from rest_framework.viewsets import ModelViewSet
from .models import Post
from .serializers import PostSerializer
class PostViewSet(ModelViewSet):
queryset = Post.objects.all()
serializer_class = PostSerializer
urls.py
from rest_framework.routers import DefaultRouter
from django.urls import path, include
from .views import PostViewSet
router = DefaultRouter()
router.register(r'posts', PostViewSet)
urlpatterns = [
path('', include(router.urls)),
]