Django Shell की मूल बातें
In this page:
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.
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.
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.
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.
exit()
⚠️ Run this in your own terminal or Node.js environment.
- एक model उसका app जाने बिना import करने की कोशिश करना, यह भूल जाना कि shell को अब भी correct
from app.models import Modelline चाहिए। - shell में changes करना और यह उम्मीद करना कि इसे बंद करने पर वे गायब हो जाएंगी, जब saved objects किसी भी दूसरे write की तरह असली database में persist करते हैं।
- Django shell को plain
pythoninterpreter के साथ 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 करने का सबसे तेज़ तरीका है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: