Python Django Tutorial: Operations on Database models - Part Two

In this article you will learn about operations on Database models in Python Django. This is part two of the article series.

Today, I will take a new part of Python Django from my tutorial series. In this part you will learn the Database model features.

I already told you the following:

Store data in Database:

  1. Firstly, follow my article.

  2. After reading this article now we move on to some extra features of models in django that will help for customization of database.

  3. So, I have already made a demo of a django project that has main django app.

  4. Open file in django app folder.

    This file has code as below:


  5. Now we want to firstly create database using migration.

  6. Open command Prompt with project root directory.

  7. Enter Commands

    python makemigrations <Enter django app name>
    python sqlmigrate <Enter django app name>
    <prefix of file which generate from above command like : 0001>
    python migrate

  8. So you have to create database table successfully then we want to save data through command prompt in database.

  9. Now Enter command in CMD : python shell. Above command is used for opening shell of demo project that gives direct access to project class and models that are shown as below image.


  10. Now I have class named UserData that is shown in code that also defines the models or Table name.

  11. Now we store the data in database using some code as below.

    a. from main.models import UserData
    b. user= UserData(emailid="",password="12345678")


Here main.models denote the file in the main folder that have UserData models. So we access that models using above code. Second code stored data in is user object. save() is predefine function that save object value into database.

user object is gives many features to see data of objects which were saved using it.

  • user.emailid
  • user.password
  • user. _get_pk_val()

Output of above code is as shown in the below image:


Access data from Database

  1. You have saved data into database. You can check it some code as below:



  2. Now you have access to information about UserData Tables.

  3. So you can access database value with many constraints using predefined methods of django.


     Output : [(2, '', '12345678')]


    Output : [(2, '', '12345678'), (3, '', '12345678')]


    Output : [{'emailid': '', 'password': '12345678', 'id': 2}]


    Output : True


  4. It is very useful for that person who doesn’t know sql queries for doing operations on Database.
Update data in Database:

Now we update the database values using code as below, 

user = UserData.objects.get(pk=2)
user.password = 'abcdefgh'

It will update the password of user that have ID = 2


Delete data in Database

Now we will do operation of deleting data from database.

  1. Firstly delete data using filter techniques that remove specific data from database.


    Output : [(3, '', '12345678')]


    Output : [ ]


  2. Delete all values from database table.


    Output : [ ]


Django has many methods for using databases directly without knowlegde of using SQL queries. I hope you understood this part of the python django tutorial.