Python Django Tutorial: Operations on Database models - Part Two


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 file in the Django app folder.
    This file has code as below:
  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 makemigrations <Enter Django app name>  
    3. python sqlmigrate <Enter django app name>  
    5. <prefix of the file which generates from the above command like 0001>  
    7. python 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 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.
  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="",password="12345678")  
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. _get_pk_val()
The output of the above code is as shown in the below image:
Access data from Database
  1. You have saved data into the 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.
    1. UserData.objects.filter(pk=2).values_list() 
    Output : [(2, '', '12345678')]
    1. UserData.objects.filter(emailid='').values_list() 
    Output : [(2, '', '12345678'), (3, '', '12345678')]
    1. UserData.objects.filter(pk=2).values_list().values() 
    Output : [{'emailid': '', 'password': '12345678', 'id': 2}]
    1. UserData.objects.filter(pk=2).values_list().exists() 
    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, 
  1. user = UserData.objects.get(pk=2)  
  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 the database.
    1. UserData.objects.values_list() 
    Output : [(3, '', '12345678')]
    1. UserData.objects.filter(pk=3).delete()  
    2. UserData.objects.values_list() 
    Output : [ ]
  2. Delete all values from the database table.
    1. UserData.objects.all().delete() 
    Output : [ ]
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.