← Back to Django Course | Chapter 5: Migrations & Database Management | Lesson 1 of 8

Django Migrations का परिचय

Migrations Django का तरीका है आपके models में किए हर change को लिखने का ताकि यह matching database tables को अपने आप build (या rebuild) कर सके।

एक Migration क्या Represent करता है

एक migration file एक छोटा Python module है जिसमें operations की एक list (जैसे CreateModel या AddField) होती है जिन्हें Django आपके database के लिए SQL में translate करता है।

हर app अपनी migrations को एक app/migrations/ folder में रखता है, उस order में numbered जिसमें उन्हें चलना चाहिए।

Note: Migrations सिर्फ Python code हैं — आप इन्हें किसी भी दूसरी file जैसे खोल और पढ़ सकते हैं।

उदाहरण: 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.

markup
# 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 करने देते हैं।

Warning: Database में table को directly edit न करें जब आपकी models.py कुछ अलग कहती है — दोनों drift हो जाएंगे।

उदाहरण: Why Migrations Exist

This Article model is the source of truth; a migration is generated from it, never written from scratch by hand.

markup
# 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 से चलाए जाते हैं।

Note: हर model change के बाद makemigrations चलाएं, एक field rename करने जैसे छोटे change के बाद भी।

उदाहरण: The Two-Step Migration Workflow

makemigrations creates the migration file describing the change; migrate then executes it against the database.

bash
python manage.py makemigrations
python manage.py migrate

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. Model बदलने और एक migration बनाने के बजाय database tables को हाथ से edit करना, जो Django की migration history को असली schema से out of sync छोड़ देता है।
  2. migrations/ folder से migration files delete करना क्योंकि वे messy दिखती हैं, जो हर दूसरे environment के लिए migrate तोड़ देता है जो अब भी उन्हें expect करता है।
  3. यह मान लेना कि एक 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 हैं।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.