-2

I have a design question on django.

Can someone explain to me why the Django ORM 'get' doesn't return a queryset? To the best of my understanding, a queryset should/is the result of a db query. With that logic, isn't a get query a queryset?

Also, from my research towards this question, I found out the get query calls the model manager while the queryset doesn't.

Is there a reasoning behind all of this?

Fadil Olamyy Wahab
  • 626
  • 2
  • 6
  • 15
  • 1
    https://stackoverflow.com/questions/3221938/difference-between-djangos-filter-and-get-methods – souldeux Nov 05 '17 at 15:05
  • Possible duplicate of [Difference between Django's filter() and get() methods](https://stackoverflow.com/questions/3221938/difference-between-djangos-filter-and-get-methods) – ChrisGPT was on strike Nov 05 '17 at 15:20

1 Answers1

0

From django documentation queryset api reference

get() raises MultipleObjectsReturned if more than one object was found. The MultipleObjectsReturned exception is an attribute of the model class.

While filter returns a set of instances that match the lookups kwargs in an instance of QuerySet

For example:

A=Model.objects.get(id=1)
B=Model.objects.filter(id=1)
print(isinstance(A,Model))  # True
print(isinstance(B,QuerySet))  # True

Get sql >>

select * from public.app_model where id=1;

Filter sql >>

select * from public.app_model where id=1;

but at implementation get must return an instance, if the query returns more than 1 row it will raise MultipleObjectsReturned yet filter not, it will return all the rows in Model class instance form

BTW, Both use the cache property

A.Raouf
  • 2,171
  • 1
  • 24
  • 36