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

Django Shell की मूल बातें

Django shell एक regular Python prompt है जो आपके models के बारे में पहले से जानता है, इसलिए आप एक script लिखने के बजाय commands type करके अपना data query और change कर सकते हैं।
Syntax
markup
python manage.py shell

>>> from app_name.models import ModelName
>>> ModelName.objects.all()

Shell खोलना

manage.py shell चलाना Django की settings पहले से loaded वाला एक interactive Python session शुरू करता है, इसलिए models और database तुरंत उपयोग के लिए ready हैं।

उदाहरण: Opening the Shell

This drops you into a Python prompt (>>>) where Django is fully configured — no manual setup needed before importing models.

bash
python manage.py shell

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

एक Model Import और Query करना

Shell के अंदर आप एक model वैसे ही import करते हैं जैसे एक views.py file में करते, फिर उस पर सीधे objects.all() जैसे methods call करते हैं।

उदाहरण: Importing and Querying a Model

This prints a QuerySet showing every Article row currently stored in the database.

bash
from blog.models import Article
Article.objects.all()

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

Objects बनाना और Save करना

आप shell से सीधे एक नया model instance बना सकते हैं और इस पर .save() call कर सकते हैं, जो इसे तुरंत database में लिखता है, बिल्कुल एक view की तरह।

उदाहरण: Creating and Saving Objects

After save() runs, a new row exists in the article table — this change is permanent, not undone by closing the shell.

bash
a = Article(title='My First Post')
a.save()

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

Shell से बाहर निकलना

exit() type करना या Ctrl+D दबाना shell बंद करता है और आपको अपने regular terminal prompt पर वापस ले जाता है; आपके saved data के बारे में कुछ भी नहीं खोता।

उदाहरण: Exiting the Shell

This ends the interactive session; any objects already saved with .save() remain in the database afterward.

bash
exit()

⚠️ 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 उसका app जाने बिना import करने की कोशिश करना, यह भूल जाना कि shell को अब भी correct from app.models import Model line चाहिए।
  2. shell में changes करना और यह उम्मीद करना कि इसे बंद करने पर वे गायब हो जाएंगी, जब saved objects किसी भी दूसरे write की तरह असली database में persist करते हैं।
  3. Django shell को plain python interpreter के साथ confuse करना, जिसे Django explicitly set up होने तक आपके models तक कोई access नहीं है।
चैप्टर सारांश
  • python manage.py shell एक ऐसा Python prompt खोलता है जिसमें Django पहले से configured है।
  • आप किसी भी model को exactly वैसे ही import और query कर सकते हैं जैसे एक view में करते।
  • आप shell में जो changes save करते हैं वे database में real, permanent writes हैं।
  • यह बिना एक view लिखे ORM के साथ experiment करने का सबसे तेज़ तरीका है।

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.