[Django]-Django – Find out which model the variable belongs to

5👍

Passing an object to type() will tell you the type of the object.

print type(some_model) 

8👍

Test if some_model is of type inherited from MyType

issubclass(type(some_model), MyType)

Test if some_model is instance of MyType

isinstance(some_model, MyType)
👤Ilya

0👍

For if statements this worked for me

from app.models import SomeModel
model_var = SomeModel()
if type(model_var) == SomeModel:
    #Do stuff
    pass
👤Jordan

Leave a comment