Introduction
Well, this article is actually the prerequisite article providing necessary information which is important for another several articles. Namely, for getting Django object, retrieving specific Django object or create and insert data to the database using Django object, also get value from Django object’s propertyor attribute. Look at How to Create or Insert Data using Django Model from a Django Application using Python Interactive Shell, How to Get certain Object from a Django Model from a Django Application using Python Interactive Shell or How to Get Property or Attribute Value of a Django Object from a Django Application using Python Interactive Shell. But before going on to those kinds of action, it is important to access the Django Model for further usage. Below is the environment description in order to simulate it :
-
An environment where the device have a Python tool interpreter in it. Install it by looking in How to Install Python in Microsoft Windows, How to Install Python 3.10 in Microsoft Windows or How to Install Python in Microsoft Windows 11
-
Also, an environment where the device has ‘pip’ tool in it. Just install it by following in How to Install pip in Microsoft Windows or How to Install pip in Microsoft Windows 11 as references.
-
Moreover, an environment where it has a python virtual environment to isolate and run the Django application. As to create it, look in How to Create an Isolated Python Environment, How to Create a Python Virtual Environment with a specific version of Python in Microsoft Windows or in How to Specify Specific Python Version for Creating a Virtual Python Environment in Microsoft Windows.
-
Furthermore, since there is a virtual python virtual environment, activate it.
-
Next, in the python virtual environment, install Django library. In How to Install Django in Microsoft Windows, there is an info do perform it.
-
Last, in order to complete the environment, just create Django project and a Django application inside of it. More reference on that matter, check How to Create a Django Application inside a Django Project in Microsoft Windows.
How to Access and Use Django Model of a Django Application using Python Interactive Shell in Microsoft Windows
What is the term access and use a Django model ?. Basically, in order to use a Django model for getting into the data processing from the database, Django model act as an database abstraction layer. It allow the Django application retrieving and processing data from and to the database. Each record of the data has the representation of the Django model which is known as Django object. But before getting on to the Django object to be able to process the data from and to the database, it need an access to the Django model. Below is the steps in order to achieve it :
-
Off course, the first step will be running the Python interactive shell as follows :
(env) C:\django\myproject>python manage.py shell Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>>
-
Finally, since it is already in a Python interactive shell, start access the Django model. As an example, in this article, it will use a Django model which is exist in the Django library by default. That Django model is the User Django model. But before that User Django model exist and it is available for further usage, there is a necessary command execution. It is importing the User Django model. In order to be able to import it correctly, the knowledge of the exact package library path is also a must. Below is an example to do it :
>>> from django.contrib.auth.models import User; >>>
-
And then, since the import process is a success, it is possible to do any process or operation using that Django model. Below are several examples of it :
>>> user_list = User.objects.all() >>> print(user_list); <QuerySet [<User: frank.lieberman>, <User: jack.morgan>, <User: dave.hamilton>, <User: admin>]> >>> user_list = User.objects.create_user('john.jameson','john@mycompany.com','johnpassword'); >>> user_list = User.objects.all() >>> print(user_list); <QuerySet [<User: frank.lieberman>, <User: jack.morgan>, <User: dave.hamilton>, <User: admin>, <User:john.jameson>]>>>>