Anupam
Anupam

Reputation: 15610

Check if an object is a QuerySet

I have an object variable obj. Is it possible to check whether its a Queryset or not?

Upvotes: 26

Views: 13648

Answers (2)

Ajmal Noushad
Ajmal Noushad

Reputation: 946

You can get the type of obj by python's inbuilt type()

Try this :

  type(obj)

It will return as below if its a Queryset

  <class 'django.db.models.query.QuerySet'>

Upvotes: 5

Dima  Kudosh
Dima Kudosh

Reputation: 7366

You can use python built-in function isinstance.

from django.db.models.query import QuerySet
isinstance(your_qs, QuerySet)

Upvotes: 57

Related Questions