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

makemigrations नामक Command

makemigrations आपके models को देखता है और एक set of instructions लिखता है जो बताता है कि last time से क्या changed है।
Syntax
markup
python manage.py makemigrations
python manage.py makemigrations app_name

makemigrations चलाना

अपने project के root folder से (जहां manage.py रहता है), makemigrations चलाना आपके current models.py को last recorded migration से compare करता है और जो भी changed हो उसके लिए एक नई file लिखता है।

उदाहरण: Running makemigrations

Django scans all installed apps and prints something like 'Migrations for blog: 0002_article_published.py' for each change it found.

bash
python manage.py makemigrations

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

एक Single App को Target करना

makemigrations के बाद एक app का नाम pass करना scan को सिर्फ उस app तक restrict करता है, जो कई apps वाले एक बड़े project में उपयोगी है।

उदाहरण: Targeting a Single App

Only the blog app's models are checked; other installed apps are left untouched even if they also changed.

bash
python manage.py makemigrations blog

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

Output पढ़ना

makemigrations app का नाम और जो नई migration filename इसने बनाई वह print करता है, साथ ही हर operation क्या करता है इसकी एक छोटी list, ताकि change apply करने से पहले आप इसे sanity-check कर सकें।

Warning: Printed operation list को ध्यान से पढ़ें — यह बताती है कि आपके tables के साथ exactly क्या होगा।

उदाहरण: Reading the Output

The comment lines show Django's own explanation of the change it detected, matching the new BooleanField added to the model.

bash
python manage.py makemigrations
# Migrations for 'blog':
#   blog/migrations/0002_article_published.py
#     - Add field published to article

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

एक Migration को Name देना

--name flag आपको Django के auto-generated नाम के बजाय migration को एक meaningful नाम देने देता है, जो बाद में migrations folder को पढ़ना आसान बनाता है।

उदाहरण: Naming a Migration

The resulting file is named 0002_add_published_flag.py instead of an auto-generated description.

bash
python manage.py makemigrations blog --name add_published_flag

⚠️ 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. makemigrations चलाना लेकिन बाद में migrate चलाना भूल जाना, इसलिए database को असल में change कभी मिलता ही नहीं।
  2. एक model field का नाम directly edit करना और यह मान लेना कि Django silently इसे एक rename के रूप में detect कर लेगा, बजाय यह पूछने के कि यह rename है या नई field।
  3. एक app के लिए makemigrations चलाना और यह उम्मीद करना कि यह unrelated apps में भी changes पकड़ ले जिन्हें touch नहीं किया गया।
चैप्टर सारांश
  • makemigrations INSTALLED_APPS में हर app को model changes के लिए scan करता है।
  • यह उस app के migrations/ folder में एक नई numbered file लिखता है।
  • यह खुद database को touch नहीं करता — वह migrate का काम है।
  • आप नाम से एक single app को target कर सकते हैं ताकि सिर्फ वही app check हो।

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.