Django Migrations का परिचय
In this page:
एक Migration क्या Represent करता है
एक migration file एक छोटा Python module है जिसमें operations की एक list (जैसे CreateModel या AddField) होती है जिन्हें Django आपके database के लिए SQL में translate करता है।
हर app अपनी migrations को एक app/migrations/ folder में रखता है, उस order में numbered जिसमें उन्हें चलना चाहिए।
उदाहरण: What a Migration Represents
This migration creates an Article table with an id and a title column — exactly what Django generates the first time you run makemigrations on a new model.
# migrations/0001_initial.py
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
operations = [
migrations.CreateModel(
name='Article',
fields=[
('id', models.AutoField(primary_key=True)),
('title', models.CharField(max_length=200)),
],
),
]
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Migrations क्यों Exist करते हैं
Migrations के बिना आपको हर बार model बदलने पर हाथ से raw SQL लिखना होता, और हर teammate की database को manually sync रखना होता। Migrations Django को आपके लिए वह SQL generate करने और इसे हर जगह consistently apply करने देते हैं।
उदाहरण: Why Migrations Exist
This Article model is the source of truth; a migration is generated from it, never written from scratch by hand.
# models.py
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=200)
published = models.BooleanField(default=False)
def __str__(self):
return self.title
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Two-Step Migration Workflow
Django migrations को दो commands में split करता है: makemigrations आपके models पढ़ता है और migration file लिखता है, फिर migrate इसे असल में database पर apply करता है। दोनों आपके project के अंदर से terminal से चलाए जाते हैं।
उदाहरण: The Two-Step Migration Workflow
makemigrations creates the migration file describing the change; migrate then executes it against the database.
python manage.py makemigrations
python manage.py migrate
⚠️ Run this in your own terminal or Node.js environment.
- Model बदलने और एक migration बनाने के बजाय database tables को हाथ से edit करना, जो Django की migration history को असली schema से out of sync छोड़ देता है।
- migrations/ folder से migration files delete करना क्योंकि वे messy दिखती हैं, जो हर दूसरे environment के लिए migrate तोड़ देता है जो अब भी उन्हें expect करता है।
- यह मान लेना कि एक migration उसी moment चलता है जब आप एक model change save करते हैं, जब यह असल में makemigrations चलाने के बाद ही exist करता है।
- एक migration एक Python file है जो आपके models में एक change describe करती है, जैसे एक table या एक field add करना।
- Django migrations की एक पूरी history रखता है ताकि database किसी भी machine पर scratch से rebuild हो सके।
- हर model change को database में effect होने से पहले एक नई migration चाहिए।
- Migrations आपके Python models और असली SQL tables के बीच bridge हैं।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: