Instagram
youtube
Facebook
Twitter

Django ORM Queries

Django ORM makes it very easy to interact with our database. We can even do CRUD operations using this feature. In this tutorial, we’ll use Django ORM API to interact with our database and do some operations.

Create Object

Creating or Adding something to the database is one of the most important operations to do in a database. Use the following command to create an object in the database.

Contact.objects.create(name='codersdaily', email='contact@gmail.com', phone='98989998', message='hey')

here, we used Contact.objects.create command to create an object in the database. Let's check if our credentials are saved in the database or not.

 

Working properly.

Retrieving Single Objects

Django ORM has an in-built queryset to retrieve some specific data from the database. Use the following command to do this operation.

>>> queryset = Contact.objects.get(phone='98989998')
>>> queryset
<Contact: Contact object (3)>

Here, we have used Contact.objects.get method to retrieve the name of the contact who has given a phone number.

 

Ordering Objects

We can also order our objects in a sequence with the help of Django ORM. use the following command to do this.

>>> Contact.objects.order_by('phone')
<QuerySet [<Contact: Contact object (1)>, <Contact: Contact object (2)>, <Contact: Contact object (3)>]>
>>> 

Here, we have used Contact.objects.order_by method to order our objects.

 

Retrieving Specific Object Values

By using Django ORM we can also retrieve some specific object values. Use the following command to do this.

>>> Contact.objects.values('name', 'email')
<QuerySet [{'name': 'codersdaily', 'email': 'contact@gmail.com'}, {'name': 'rohit', 'email': 'contact@mail.com'}, {'name': 'codersdaily', 'email': 'contact@gmail.com'}]>
>>> 

Here, we have used Contact.objects.values method and passed the objects to retrieve specific data.

 

Filter Objects

One of the main advantages of using Django ORM is the ability to filter objects. There are many filters we can apply to our objects. Some practical examples are given below.

>>> queryset = Contact.objects.filter
(name__startswith = 'C')
>>> queryset
<QuerySet [<Contact: Contact object (1)>, <Contact: Contact object (3)>]>
>>>

Here, we have used Contact.objects.filter method to filter the Contacts whose name starts with C.

 

Slicing Objects

We all know about Slicing in python. We can do the same stuff in Django also. We can make limits and display specific objects. Use the following method to do this.

>>> Contact.objects.all()[:2]
<QuerySet [<Contact: Contact object (1)>, <Contact: Contact object (2)>]>
>>>

Here, we have used Contact.objects.all()[:2] Method to slice our objects.

 

Deleting Objects

You can delete any specific or all object records using Django ORM. use the following command to do this.

>>> Contact.objects.all().delete()
(3, {'demo.Contact': 3})
>>> 

 

Here, we have used Contact.objects.all().delete() Command to delete all objects from our Contact. 

To exit Django shell you can use exit() command.