Migration Files की व्याख्या
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.
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.
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 के बीच भी।
उदाहरण: Why Order Matters
This lists every migration in the blog app with an [X] next to ones already applied, showing the real applied order.
python manage.py showmigrations blog
⚠️ Run this in your own terminal or Node.js environment.
- Migration files को manually renumber या rename करना, जो उनके बीच Django की dependency tracking तोड़ता है।
- यह मान लेना कि एक migration file की operations list current model से exactly match करती है, जब कई migrations हर एक अपने छोटे incremental changes कर सकती हैं।
- एक ऐसी 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 करेगी।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: