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 database.

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

  4. Open models.py file in django app folder.

    This file has code as below:

    file

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

  6. Open command Prompt with project root directory.

  7. Enter Commands

    python manage.py makemigrations <Enter django app name>
    python manage.py sqlmigrate <Enter django app name>
    <prefix of file which generate from above command like : 0001>
    python manage.py 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 mange.py 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.

    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 database using some code as below.

    a. from main.models import UserData
    b. user= UserData(emailid="[email protected]",password="12345678")
    c. user.save()

    cmd

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.pk
  • user. _get_pk_val()

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

output

Access data from Database

  1. You have saved data into 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.

     UserData.objects.filter(pk=2).values_list()

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

    UserData.objects.filter(emailid='[email protected]').values_list()

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

    UserData.objects.filter(pk=2).values_list().values()

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

    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, 

user = UserData.objects.get(pk=2)
user.password = 'abcdefgh'
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 database.

    UserData.objects.values_list()

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

    UserData.objects.filter(pk=3).delete()
    UserData.objects.values_list()

    Output : [ ]

    cmd

  2. Delete all values from database table.

    UserData.objects.all().delete()

    Output : [ ]

    cmd

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.

Up Next
    Ebook Download
    View all
    Learn
    View all