Django best way to check the model type of a queryset

wobbily_col picture wobbily_col · May 29, 2013 · Viewed 8.9k times · Source

I have a Django action function which I would like to use on querysets based on different models.

What is the best way to check the model type my queryset is composed of? Say I want to check for a Library class that is defined in my models.py

At the moment I can get it to work using

for object in queryset :
    if object.__class__.__name__  == "Library"

But I am sure there is a better way of doing this.

I assume somehow I do something using queryset.model. I have tried the following, but it doesn't do what I want it to:

import myapp.models.Library

def my function(modeladmin,request  queryset )

    if isinstance(queryset.model , Library ) :
        # do something specific here

Answer

wobbily_col picture wobbily_col · May 29, 2013

Ok, I see, I use is instead of isinstance():

if queryset.model is Library :
    # do something.