QuerySet API Methods that return new QuerySets in Django ✅

 Methods that return new QuerySets


1) all() - This method is used to retrieve all objects(rows).

Example: Student.objects.all()

Here Student is the name of model name 


2) filter() - This method is used to fetch specific data from database table 

Example: Student.objects.filter(id=5)

It returns the object(row)  of id =5

3) exclude() - This method is used to fetch the data that do not match the given lookup parameters

Example: Student.objects.exclude(id=5)

It returns the object(row) that does not match the id=5 i.e. id!=5

4) order_by(column_name) - It orders the field.

column_name =  Asc order
-column_name = Desc order
? =  Randomly

Example: Student.objects.order_by(id)

It returns the row by ascending order.

5) values(column_name) - It fetch the specific column from db table.

Example: Student.objects.values('name','city')


6) distinct(column_name) This eliminates the duplicate rows from the query results.

Example: Student.objects.distinct('name')

7) values_list(column_1,column_2,named=True) - This is similar to values () except  that returning dictionaries, it returns tuple when iterated over.


Post a Comment

0 Comments