13

Im a newbie to Django and would like to understand what is the difference between filter vs get

Get

Entry.objects.get(id__exact=14)

Filter

Entry.objects.filter(id__exact=14)

What difference the above statement makes?

Thanks in advance.

user1050619
  • 19,822
  • 85
  • 237
  • 413
  • 1
    Duplicate of http://stackoverflow.com/questions/1541249/difference-between-get-and-filter-in-django-model-layer – Michael C. O'Connor Mar 06 '13 at 15:32
  • Also Duplicate of [Difference between Django's `filter()` and `get()` methods](http://stackoverflow.com/questions/3221938/difference-between-djangos-filter-and-get-methods) – Grijesh Chauhan Nov 22 '13 at 04:39
  • Possible duplicate of [Difference between GET and FILTER in Django model layer](http://stackoverflow.com/questions/1541249/difference-between-get-and-filter-in-django-model-layer) – Ajay Singh Mar 27 '17 at 14:15
  • Does this answer your question? [Difference between Django's filter() and get() methods](https://stackoverflow.com/questions/3221938/difference-between-djangos-filter-and-get-methods) – natka_m Jul 12 '20 at 14:26
  • Also, the answers to the earlier questions are of better quality than the ones given here... – natka_m Jul 12 '20 at 14:27
  • I wish google was as good at finding these previous answers as you guys were. This page is the top hit in 2020 – logan Nov 25 '20 at 17:50

4 Answers4

13

the get only brings an element that is equal to what you're looking for but the filter brings everything related to that item you want.

filter returns many things found. get returns only one thing to what you're looking for

for example:

GET

Task.objects.get(id=1,status=1)

Filter

Groups.objects.filter(user=1)
Mushahid Khan
  • 2,816
  • 1
  • 19
  • 32
alfonsoolavarria
  • 1,141
  • 11
  • 11
2

If you know it's one object that matches your query, use get. It will fail if it's more than one, and gives the error like this:

Traceback (most recent call last):

    File "<console>", line 1, in <module>
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 143, in         get
    return self.get_query_set().get(*args, **kwargs)
    File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 407, in get
    (self.model._meta.object_name, num))
    MultipleObjectsReturned: get() returned more than one Poll -- it returned 2!

Otherwise use filter, which gives you a list of objects.

David Buck
  • 3,752
  • 35
  • 31
  • 35
Priyank
  • 3,778
  • 3
  • 29
  • 48
2

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

 __data = User.objects.get(is_active=1).exclude(id=id)

Error:get() returned more than one User -- it returned 19!, Type:<class 'django.contrib.auth.models.MultipleObjectsReturned'>

------successful-------

__data = User.objects.filter(is_active=1).exclude(id=id)

-------successful------

Check the Link

Garf365
  • 3,619
  • 5
  • 29
  • 41
alfonsoolavarria
  • 1,141
  • 11
  • 11
2

To add to what others have said... (get returns exactly 1 record, while filter returns a set), an important thing to note is the type of the results.

get() returns an object, while filter returns a queryset type. This means you can do something like

age = User.objects.get(name="Ryan").age

Whereas if you were using filter, you'd need to do a couple extra steps:

ages = User.Objects.filter(name="ryan").values('age')
for user_age in users:
    print(user_age)

The important thing to note is that filter returns an iterable type with a values() method dictating which fields you want. Get simply returns an object with the fields as attributes, so it is much simpler to get the data you need.

  • I was about to upvote your answer, but there's a mistake in it: `get` does NOT return a queryset - it returns a single object. It is `filter` that returns a `queryset`, which is, as you correctly point out, an iterable. – natka_m Jul 12 '20 at 14:23
  • I think you have a logic error in the second code ```python ages = User.Objects.filter(name="ryan").values('age') for user_age in users: print(user_age) ``` it should be ```python ages = User.Objects.filter(name="ryan").values('age') for user_age in ages: print(user_age) ``` – NourEldin Osama Nov 29 '22 at 04:00