django model CRUD operations
Let's do CRUD operations on django model. In the previous article we have seen how to create a table or model in the django application. Let's write basic queries like insert, update and delete queries using django ORM.
Let's look at the Django Model
from django.db import models class Person(models.Model): first_name = models.CharField(max_length=100) last_name = models.CharField(max_length=100) address = models.TextField()
Now, let's quickly perform basic CRUD operations using the django shell. To open django shell execute the below command.
python manage.py shell
Create or insert query in Django
Django model has a default query manager which has the API to perform the queries. Let's create a "Person" object using below code.
from .models import Person # query1 obj1 = Person(first_name="Pradeep", last_name="K", address="Hyderabad") obj1.save() # query2 obj2 = Person.objects.create(first_name="Anji", last_name="B", address="Warangal")
In the above code we have written queries to insert the data into the table using django ORM. If you observe the above code we have done it in two ways. In the first query we have create the person object then called the save method to save the object into the database table. In the second query we have used django ORM manager method "create" to insert the data into the database table. Whenver a object is created django will automatically assigns a primary key "id" to the record which is used to identify the record uniquely.
Get or Retrieve query in Django
Django query manager has the method "get" to retrieve the data from the database table. Let's see some code
from .models import Person obj1 = Person.objects.create(first_name="John", last_name="B", address="Hyd") print(obj.id) # 1 obj2 = Person.objects.get(id=1) print(obj2.first_name) # John
In the above code we first created the record and printed it's "id" number. After that we have retrieved the record from the database using django model manger method "get" by passing id. We can also use the other parameters (i.e "
Update query in Django
We can update a record the database table using the django model managers method "save" like below. Let's do some code
from .models import Person obj1 = Person.objects.get(id=1) obj1.first_name = "Adam" obj1.save()
To update the record the database table we need to retrieve the record first and make some changes to it and then call the "
save" method on object.
Delete query in Django
To delete the object from the database django model manager provide a "
delete" method to perform the delete operation.
from .models import Person # query1 obj1 = Person.objects.get(id=1) obj1.delete() # query2 Person.objects.filter(id=1).delete()
We can perform the delete operation in two ways like above code.
Filter query in Django
Django makes filtering of objects or records very easy using the model manager method "
filter". Let's do some code
from .models import Person # create queries Person.objects.create(first_name="John Snow", last_name="D", address="Hyderabad") Person.objects.create(first_name="John Wind", last_name="D", address="Banglore") Person.objects.create(first_name="Carl Miller", last_name="R", address="Banglore") # filter query obj_list = Person.objects.filter(first_name__icontains="John") print(obj_list)
Above query filters the objects whose first name contains the word "john".
Note: While doing the operations you can check the database table to confirm whether it's working or not. If you use postgreSQL database then you can use "pgAdmin" or if you use MySQL database then you can use "MySQL Workbench" for administration.
That's it folks. We can do more with django ORM. Let's do more django queries in the next article.