Python Django Tutorial: Operations on Database models - Part Two

Introduction

 
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 the database.
     
  3. So, I have already made a demo of a Django project that has the main Django app.
     
  4. Open models.py file in the Django app folder.
     
    This file has code as below:
     
    file
     
  5. Now we want to first create a database using migration.
     
  6. Open Command Prompt with the project root directory.
     
  7. Enter Commands
    1. python manage.py makemigrations <Enter Django app name>  
    2.    
    3. python manage.py sqlmigrate <Enter django app name>  
    4.    
    5. <prefix of the file which generates from the above command like 0001>  
    6.    
    7. python manage.py migrate  
  8. So you have to create a database table successfully then we want to save data through command prompt in the database.
     
  9. Now Enter command in CMD: python mange.py shell. The above command is used for opening a shell of a demo project that gives direct access to project class and models that are shown as below image.
     
    demo
     
  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 the database using some code as below.
    1. from main.models import UserData  
    2. user= UserData(emailid="[email protected]",password="12345678")  
    3. user.save() 
    cmd
Here main.models denote the file in the main folder that has UserData models. So we access those models using the above code. The second code stored data in is user object. save() is a predefined function that saves object value into the database.
 
the user object is given many features to see data of objects which were saved using it.
  • user.emailid
  • user.password
  • user.pk
  • user. _get_pk_val()
The output of the above code is as shown in the below image:
 
output
 
Access data from Database
  1. You have saved data into the database. You can check it some code as below:
     
    UserData.objects.values_list()
     
    code
     
  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.
    1. UserData.objects.filter(pk=2).values_list() 
    Output : [(2, '[email protected]', '12345678')]
    1. UserData.objects.filter(emailid='[email protected]').values_list() 
    Output : [(2, '[email protected]', '12345678'), (3, '[email protected]', '12345678')]
    1. UserData.objects.filter(pk=2).values_list().values() 
    Output : [{'emailid': '[email protected]', 'password': '12345678', 'id': 2}]
    1. UserData.objects.filter(pk=2).values_list().exists() 
    Output: True
     
    cmd
     
  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, 
  1. user = UserData.objects.get(pk=2)  
  2. user.password = 'abcdefgh'  
  3. user.save() 
It will update the password of user that have ID = 2
 
cmd
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 the database.
    1. UserData.objects.values_list() 
    Output : [(3, '[email protected]', '12345678')]
    1. UserData.objects.filter(pk=3).delete()  
    2. UserData.objects.values_list() 
    Output : [ ]
     
    cmd
     
  2. Delete all values from the database table.
    1. UserData.objects.all().delete() 
    Output : [ ]
     
    cmd
Django has many methods for using databases directly without knowledge of using SQL queries. I hope you understood this part of the python Django tutorial.


Similar Articles