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

Migrations से Models में बदलाव करना

जब आप एक model change करते हैं — जैसे एक नई field जोड़ना — Django एक नई migration लिखता है जो existing table को match करने के लिए update करती है।
Syntax
markup
# 1. Change the model (add, remove or alter a field)
# 2. Generate the migration
python manage.py makemigrations
# 3. Apply it
python manage.py migrate

एक नई Field जोड़ना

एक model में field जोड़ना और makemigrations चलाना एक AddField operation generate करता है। अगर existing rows मौजूद हों और field nullable न हो, तो Django makemigrations के दौरान एक one-time default value के लिए prompt करेगा।

उदाहरण: Adding a New Field

Giving subtitle a default='' means existing Article rows get an empty subtitle automatically instead of makemigrations asking for one interactively.

markup
class Article(models.Model):
    title = models.CharField(max_length=200)
    subtitle = models.CharField(max_length=200, default='')
{# 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. #}

एक Field हटाना

model से एक field delete करना और makemigrations चलाना एक RemoveField operation generate करता है, जो migrate चलने पर उस column को — और इसका सारा data — table से drop कर देता है।

Warning: एक बार RemoveField migration apply हो जाए, उस column का data recover नहीं किया जा सकता।

उदाहरण: Removing a Field

After removing a field from Article in models.py, this pair of commands generates and then applies the RemoveField migration.

bash
python manage.py makemigrations blog
python manage.py migrate blog

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

एक Field का नाम बदलना

जब makemigrations detect करता है कि एक field हटाई गई और उसी run में एक similarly-typed field जोड़ी गई, यह पूछता है कि क्या यह असल में एक rename है, जो existing data को खोने के बजाय preserve करता है।

Note:
  • Existing data रखने के लिए rename prompt पर y answer करें
  • और n answer करने से एक नया empty column बनता है।

उदाहरण: Renaming a Field

Confirming the rename produces a RenameField operation, which keeps every existing row's data instead of dropping and recreating the column.

bash
python manage.py makemigrations
# Did you rename article.title to article.headline? [y/N]

⚠️ 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. एक required (non-nullable) field एक ऐसे model में जोड़ना जिसमें पहले से rows हों, बिना Django को उन existing rows के लिए fill करने हेतु एक default value दिए।
  2. models.py में एक field का नाम rename करना और makemigrations को इसे rename के रूप में confirm करने के बजाय एक remove-plus-add के रूप में बनने देना।
  3. यह भूल जाना कि एक field हटाने वाली migration migrate चलने पर उस column का data permanently delete कर देगी।
चैप्टर सारांश
  • कोई भी model change — नई field, हटाई गई field, बदला हुआ type — को अपनी migration चाहिए।
  • Existing rows वाली table में एक required field जोड़ने के लिए एक default value चाहिए होती है।
  • makemigrations जब rename बनाम add/remove बता न सके तो interactively पूछता है।
  • production database पर migrate चलाने से पहले हमेशा generated migration review करें।

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.