Django Tutorial · Django Tutorial

Django Insert Data

Learn all about Django Insert Data in this comprehensive tutorial.

5 min read intermediate
  • The Members table created in the previous chapter is empty.
  • You can add multiple records by making a list of Member objects, and execute .

Add Records

The Members table created in the previous chapter is empty.

We will use the Python interpreter (Python shell) to add some members to it.

To open a Python shell, type this command:

python

Now we are in the shell, the result should be something like this:

python

At the bottom, after the three >>> write the following:

python

Hit [enter] and write this to look at the empty Member table:

python

This should give you an empty QuerySet object, like this:

python

A QuerySet is a collection of data from a database.

Read more about QuerySets in the Django QuerySet chapter.

Add a record to the table, by executing these two lines:

python

Execute this command to see if the Member table got a member:

python

Hopefully, the result will look like this:

python

Add Multiple Records

You can add multiple records by making a list of Member objects, and execute .save() on each entry:

python

Hit [enter] one more time at the end to exit the for loop.

Now, if you run this command:

python

you will see that there are 6 members in the Member table:

python

Module quiz

2 questions
1

Which of the following is true about Django Insert Data?

2

What is the most common pitfall when working with Django Insert Data?

Answer all questions to submit.