Operations on Database models

Introduction

 
In this chapter, we will learn various operations that can be performed on database models.
 
Store data in Database:
  1. Now we will see some extra features of models in Django that will help for customization of the database.
     
  2. So, I have already made a demo of a Django project that has the main Django app.
     
  3. Open models.py file in the Django app folder.
     
    This file has code as below:
     
    file
     
  4. Now we want to first create a database using migration.
     
  5. Open Command Prompt with the project root directory.
     
  6. 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  
  7. So you have to create a database table successfully then we want to save data through command prompt in the database.
     
  8. 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
     
  9. Now I have a class named UserData that is shown in code that also defines the models or Table name.
     
  10. Now we store the data in the database using some code as below.
    1. from main.models import UserData  
    2. user= UserData(emailid="sainineeraj1234@gmail.com",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, 'sainineeraj1234@gmail.com', '12345678')]
    1. UserData.objects.filter(emailid='sainineeraj1234@gmail.com').values_list() 
    Output : [(2, 'sainineeraj1234@gmail.com', '12345678'), (3, 'sainineeraj1234@gmail.com', '12345678')]
    1. UserData.objects.filter(pk=2).values_list().values() 
    Output : [{'emailid': 'sainineeraj1234@gmail.com', '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 the user that has ID = 2
 
cmd
Delete data in Database
 
Now we will do the operation of deleting data from the database.
  1. Firstly delete data using filter techniques that remove specific data from the database.
    1. UserData.objects.values_list() 
    Output : [(3, 'sainineeraj1234@gmail.com', '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.
 

Summary 

 
In the next chapter, we will discuss view and URL handling. 
Author
Neeraj Kumar
296 8.1k 2m
Next » Views and URL Handling