How to Create or Insert Data using Django Model from a Django Application using Python Interactive Shell

Posted on

Introduction

This article will focus on how to create or insert data using Django model from a Django application. The process will use a tool for helping on the create or insert command execution. The tool is a python interactive shell. Actually, it is a shell which is available and exist and run and it open without having the shell terminate immediately. So, the following is the preparation to achieve it :

  1. First of all, it is obvious as a primary preparation having a Python tool interpreter is a must. Just read for a reference on python installation in Windows operating system. It 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. Next, just install ‘pip’ tool to create virtual python environment. In order to do that, just check How to Install pip in Microsoft Windows or How to Install pip in Microsoft Windows 11 for a reference.

  3. Using ‘pip’, create a virtual python environment and activate it. As for information on how to do it just check several articles about it. It exist in How to Create an Isolated Python Environment or How to Create a Python Virtual Environment with a specific version of Python in Microsoft Windows. It also exist in How to Specify Specific Python Version for Creating a Virtual Python Environment in Microsoft Windows.

  4. Inside the virtual python environment which is active, install Django library. Follow in How to Install Django in Microsoft Windows as an alternative to see how to do it.

  5. Finally, have a Django project where it exist a Django application inside of it. Check How to Create a Django Application inside a Django Project in Microsoft Windows as an alternative example.

How to Create or Insert Data using Django Model from a Django Application using Python Interactive Shell

This part of the article will be the main focus of the content. After all of the preparation, just execute the following command to get in to the python interactive shell which is available. There are several steps to accomplish it. Below are those steps :

  1. With the help of the python tool exist in the 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. In this case, the model for simulating to create or insert the data exist as one of the default model available in Django application. It is the ‘User’ model. So, below is the first execution of the command. It is a command for importing the model first :

    >>> from django.contrib.auth.models import User;
    >>> user_list = User.objects.all()
    >>> print(user_list);
    <QuerySet [<User: frank.lieberman>, <User: jack.morgan>, <User: dave.hamilton>, <User: admin>]>
    

    In the above script command execution, after successfully importing ‘User’ model, there is another script command execution. It is listing the available User by getting all objects from the ‘User’ model and then print it.

  3. Finally, the main part which is creating or inserting data, the following is the script command execution :

    >>> 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>]> >>> object.save()
    

    The above script command execution is jsut an example. Apparently, the user model has a parameter which is accepting name, email and also password. In other words, depend on the model, it will also have a different command script for executing the command on creating or inserting data. After that, there are several script command above which is getting all the objects from the model and then print it once more. It is only to show that the data from creating and inserting in the last one is also available as part of the objects of the model.

Leave a Reply