How does determining subclass through hasattr trigger a db query in django's orm?
===

For example, I am using multi-table inheritance for a class Node with sub-classes ConceptNode and DerivedNode.  To determine the type of Node I am dealing with and distribute a function call down to the appropriate subclass, I often have to call hasattr like this:

    test_node = Node.objects.all()[0]
    
    if hasattr( test_node, "conceptnode"):
        test_node.conceptnode.myFunction()
    elif hasattr( test_node, "derivednode"):
        test_node.derivednode.myFunction()
    else:
       raise Exception("Not a valid type.")

I've noticed that this results in multiple db queries, which add up to really slow down some functions I've written.

I've tried another approach using try...catch which does not decrease the number of queries.

    test_node = Node.objects.all()[0]
    
    try:
        test_node.conceptnode.myFunction()
    except ObjectDoesNotExist:
        test_node.derivednode.myFunction()

My main question is: how does django determine what queries to execute here?  I don't see how hasattr is getting translated into a db query.

Also, if anyone can suggest a more efficient way to handle this (esp. from a query count perspective), that would be great too!