How to Access and Use the Python Shell with Django



Python


In this article, we show how to access and use the interactive Python shell with Django.

With the Python shell with Django, we can access elements in a database, insert new data into a database, update data in a database, etc.

In other words, we can do a lot with databases in the interactive Python shell with Django.


Accessing the Python Shell with Django

We will now show you how to access the Python shell with Django.

This is done by opening up your computer's command prompt.

In the command prompt, you specify the whole path to the project directory you created. In my project, I named it mywebsite. Therefore, I specify the full path to mywebsite.

Once you have this full path to the project directory, type into the command prompt the following line.



If you are using a Windows PC, you may have to change python to py, so that the line of code is, py manage.py shell.

Once you run this code, you should see the following on the screen.

Interactive Python shell

You should see the following, (InteractiveConsole)

You now have the interactive Python shell up in Django.

You can now do a lot with this Python shell.

We will now specifically show how to work with database.

So, to first interact with a database table that you have created, you first have to import the database table.

So, if you created an App called Articles and you created a table called Students in the models.py file, then to import this table called Students from the Articles app, the following line of code is used.



So, from this import statement, we have our database table named Students that we can now interact with.

So, we can do things such as get all items from this Students table with the following line of code.



We can add a new student to the Students table through the following lines of code.

The Students table that I created has 3 columns, firstname, lastname, and email.



So, in this line of code, we create an object of the Students table named student1. We assign student1 a first name of Joyce, a last name of Peters, and an email of abc@myemail.com

Right now, this is saved just in the memory of the shell that we are currently using. To actually save it to the database, we use the statement, student1.save(). It is now officially saved to the database, and not just the shell.

Now that this is saved, we can now access elements of this student1 object through the following lines of code.



So, now, we've accessed elements of the student1 object by calling the object (student1) and then calling the individual column: firstname, lastname, email, etc.

Realize that when you create a database in Django, a primary key is automatically created that has the default setting of autoincrement, beginning at 1.

So, to access the primary key of an object in a table, you can either specify the object followed by id or the object followed by pk (standing for primary key). They're both the same exact thing.

We'll now create another student record but do it a different way.

We'll first create an empty object and then individual set the values of the object.



So we've now created another object in the table but did a different way. We first made an empty object and then individually set all the attributes to this object.

If you now call these object attributes, you will get out the values that you specified.

Now if you run the following line to get the id attribute of this new object, student2, you get 2.



If you want to update an element in an object, you just set the attribute to the new value.

For example, if we want to change the email, maybe the student has gotten a new email, the line of code would be shown below.



Now the email has been changed from "def@myemail.com" to "xyz@myemail.com"

Lastly, an important thing to know is how to get out of this Python shell in the event you want to return to the command prompt.

And this is done through the statement, exit(), shown below. Exit the Python shell




So, you see that there is a lot that can be done through the interactive Python shell with Django.


Related Resources

How to Randomly Select From or Shuffle a List in Python



HTML Comment Box is loading comments...