How to Get certain Object from a Django Model from a Django Application using Python Interactive Shell

Posted on

Introduction

This article is actually has a specific connection with another article. That article is How to Create or Insert Data using Django Model from a Django Application using Python Interactive Shell. In that article, there is a certain command which is useful for retrieving certain object from a Django model. As also in that article, this article has also a similar content.  It is the preparation for simulating or demonstrating about how to get certain object from a Django model will also similar. Below is the summary for the preparation in order to simulate or demonstrate it :

  1. So, the first one will be having a Python tool interpreter. The link available for further explanation exist 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

  2. Following after, make sure to have ‘pip’ tool for creating virtual python environment. As an instruction to follow just look at How to Install pip in Microsoft Windows or How to Install pip in Microsoft Windows 11 for a reference.

  3. After the previous step, right after installing ‘pip’, use it to create a virtual python environment. The steps to do that are in several articles, such as How to Create an Isolated Python Environment or How to Create a Python Virtual Environment with a specific version of Python in Microsoft Windows and in How to Specify Specific Python Version for Creating a Virtual Python Environment in Microsoft Windows.

  4. And right after the activation of virtual python environment, install Django library inside of it. In order to do that, just look at How to Install Django in Microsoft Windows for more information.

  5. In the end of the preparation step, make sure to have a Django project and also a Django application inside of it. For more information, check How to Create a Django Application inside a Django Project in Microsoft Windows in order to search for a reference.

How to get certain Object from a Django Model from a Django Application using Python Interactive Shell

In order to be able to get information about certain object from a Django model, below are the steps to achieve it :

  1. In the active python virtual environment, execute the following command :

    (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)
    >>>
    
  2. Following after, just import the Django model. In this context, it is the User Django model. The execution in the Python interactive shell as follows :

    >>> from django.contrib.auth.models import User;
    
  3. Finally, in order to get or to retrieve the objects from the Django model, below are the execution of it :

    >>> user_list = User.objects.all()
    >>> print(user_list);
    <QuerySet [<User: frank.lieberman>, <User: jack.morgan>, <User: dave.hamilton>, <User: admin>, <User:john.jameson>]> 
    

Leave a Reply