33

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 
2
  • You can use modeladmin.model to find the model Commented May 29, 2013 at 10:28
  • Yes but that is the same as what queryset.model gives me. Commented May 29, 2013 at 10:30

1 Answer 1

65

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

if queryset.model is Library : # do something. 
Sign up to request clarification or add additional context in comments.

1 Comment

What exception to raise when this check fails?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.