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

Migration Files की व्याख्या

एक migration file बस एक छोटी Python file है जो step by step list करती है कि last one के बाद आपके models में क्या changed है।
Syntax
markup
from django.db import migrations, models

class Migration(migrations.Migration):
    dependencies = [
        ('app_name', '0001_initial'),
    ]
    operations = [
        migrations.AddField(model_name='model_name', name='field_name', field=models.CharField(max_length=100)),
    ]

एक Migration File की Anatomy

हर migration file एक Migration class define करती है जिसमें दो important attributes होते हैं: dependencies, जो यह list करता है कि इससे पहले कौन सी migrations चलनी चाहिए, और operations, actual changes की list जो apply करनी हैं।

उदाहरण: Anatomy of a Migration File

This migration depends on 0001_initial having already run, then adds a new published field to the existing Article table.

markup
from django.db import migrations, models

class Migration(migrations.Migration):
    dependencies = [('blog', '0001_initial')]

    operations = [
        migrations.AddField(
            model_name='article',
            name='published',
            field=models.BooleanField(default=False),
        ),
    ]
{# 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. #}

Common Operation Types

Django model change के type के आधार पर अलग-अलग operation classes generate करता है: एक brand-new model के लिए CreateModel, एक single field के लिए AddField या RemoveField, और एक field के options बदलने के लिए AlterField।

उदाहरण: Common Operation Types

This operation changes the Article model's title field to allow up to 300 characters instead of its previous length.

markup
migrations.AlterField(
    model_name='article',
    name='title',
    field=models.CharField(max_length=300),
)
{# 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. #}

Order क्यों मायने रखता है

Migrations dependency order में चलती हैं, सिर्फ filename order में नहीं — हर file की dependencies list exactly बताती है कि कौन सी migrations पहले apply होनी चाहिए, अलग-अलग apps के बीच भी।

Note: आप showmigrations command से एक app का migration graph inspect कर सकते हैं।

उदाहरण: Why Order Matters

This lists every migration in the blog app with an [X] next to ones already applied, showing the real applied order.

bash
python manage.py showmigrations blog

⚠️ 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. Migration files को manually renumber या rename करना, जो उनके बीच Django की dependency tracking तोड़ता है।
  2. यह मान लेना कि एक migration file की operations list current model से exactly match करती है, जब कई migrations हर एक अपने छोटे incremental changes कर सकती हैं।
  3. एक ऐसी migration delete करना जिस पर एक later migration depend करती है, जो migrate को एक missing-dependency error के साथ fail करा देता है।
चैप्टर सारांश
  • हर migration file का एक numbered नाम होता है और यह उसके app के migrations/ folder के अंदर रहती है।
  • एक Migration class dependencies (कौन सी migrations पहले चलनी चाहिए) और operations (असल में क्या change होता है) list करती है।
  • Operations CreateModel, AddField, और AlterField जैसी Python classes से map होते हैं।
  • एक migration file पढ़ना आपको exactly बताता है कि यह कौन सा SQL-equivalent change करेगी।

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.