Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django objects.all() vs objects.filter()

Tags:

python

django

Queryset.objects.all() return all objects and also,

Queryset.objects.filter() return all objects too.

I have two query that use Queryset.objects.filter() and i want to use it for return all objects.

Question: Are both Queryset.objects.all() and Queryset.objects.filter() performance the same?

like image 857
hadi Avatar asked Apr 03 '18 13:04

hadi


People also ask

What does objects all () do in Django?

all() Returns a copy of the current QuerySet (or QuerySet subclass). This can be useful in situations where you might want to pass in either a model manager or a QuerySet and do further filtering on the result. After calling all() on either object, you'll definitely have a QuerySet to work with.

What's the difference between get () and filter ()?

Basically use get() when you want to get a single unique object, and filter() when you want to get all objects that match your lookup parameters.

What is the purpose of filter () method in Django?

The filter() method is used to filter you search, and allows you to return only the rows that matches the search term.

Is Django filter lazy?

Django querysets are lazy The above code doesn't run any database queries. You can can take the person_set and apply additional filters, or pass it to a function, and nothing will be sent to the database. This is good, because querying the database is one of the things that significantly slows down web applications.


1 Answers

Yes, both will perform the same in a database point of view once you are not passing any arguments on the filter. The filter will execute more processing steps, once it needs to check if you passed arguments or not, but the difference will be very little.

In this case, I suppose you should use the all() instead of filter just to make your code more clear about what are you doing.

like image 146
Gregory Avatar answered Sep 21 '22 23:09

Gregory