Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8324945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T00:15:43+00:00 2026-06-09T00:15:43+00:00

For example, I am using multi-table inheritance for a class Node with sub-classes ConceptNode

  • 0

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!

EDIT: To dump the sqlite queries performed I did the following:

from django.db import connection
from django import db
db.reset_queries()
hasattr(nds[0],'conceptnode')
hasattr(nds[0],'derivednode')
connection.queries

And I got the following results:

{'sql': u'SELECT "nodes_node"."id", "nodes_node"."name", "nodes_node"."description", "nodes_node"."node_tree_id", "nodes_node"."unique_name", "nodes_node"."last_updated_timestamp", "nodes_node"."order", "nodes_node"."data_json", "nodes_node"."data_json_synchronized" FROM "nodes_node" LIMIT 1',  'time': '0.001'}
{'sql': u'SELECT "nodes_node"."id", "nodes_node"."name", "nodes_node"."description", "nodes_node"."node_tree_id", "nodes_node"."unique_name", "nodes_node"."last_updated_timestamp", "nodes_node"."order", "nodes_node"."data_json", "nodes_node"."data_json_synchronized", "nodes_conceptnode"."node_ptr_id", "nodes_conceptnode"."node_parent_id" FROM "nodes_conceptnode" INNER JOIN "nodes_node" ON ("nodes_conceptnode"."node_ptr_id" = "nodes_node"."id") WHERE "nodes_conceptnode"."node_ptr_id" = 1 ',  'time': '0.000'}
{'sql': u'SELECT "nodes_node"."id", "nodes_node"."name", "nodes_node"."description", "nodes_node"."node_tree_id", "nodes_node"."unique_name", "nodes_node"."last_updated_timestamp", "nodes_node"."order", "nodes_node"."data_json", "nodes_node"."data_json_synchronized" FROM "nodes_node" LIMIT 1',  'time': '0.001'}
{'sql': u'SELECT "nodes_node"."id", "nodes_node"."name", "nodes_node"."description", "nodes_node"."node_tree_id", "nodes_node"."unique_name", "nodes_node"."last_updated_timestamp", "nodes_node"."order", "nodes_node"."data_json", "nodes_node"."data_json_synchronized", "nodes_derivednode"."node_ptr_id", "nodes_derivednode"."node_source_id", "nodes_derivednode"."node_target_id" FROM "nodes_derivednode" INNER JOIN "nodes_node" ON ("nodes_derivednode"."node_ptr_id" = "nodes_node"."id") WHERE "nodes_derivednode"."node_ptr_id" = 1 ',  'time': '0.000'}

The first and third of these are fetching the original node object.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T00:15:45+00:00Added an answer on June 9, 2026 at 12:15 am

    You have two instances of the simple node lookup (the LIMIT 1 query) because you are accessing a queryset as a list multiple times.

    nodes = Node.objects.all()
    nodes[0]
    nodes[0]
    

    triggers two queries, whereas:

    node = Node.objects.all()[0]
    node
    node
    

    triggers one. This can seem a bit strange at first, but the key is to remember that Node.objects.all() is not evaluated (no queries are made) until you access it.

    As for why a single object lookup runs two queries, you are using multi-table inheritance. If you just have this in your model:

    class Node(models.Model):
        pass
    
    class ConceptNode(Node):
        pass
    

    Django will create two tables, with ConceptNode rows having a parent Node.

    What you’re probably looking for is an abstract base class, which will allow you to share methods and properties between multiple classes while just using a single table for each. Just add abstract = True to the parent class meta:

    class Node(models.Model):
        class Meta:
            abstract = True
    

    Finally, note that both of these queries should take almost no time, so I wouldn’t worry about it too much.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example using SQL I can do: SELECT (a+b) as c FROM table WHERE
Given the following example (using JUnit with Hamcrest matchers): Map<String, Class<? extends Serializable>> expected
I have this simple example: using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Program
I am learning and trying simple example using node.js and mongoskin. here is my
I'd like to sort multi-column list without using a table. I know of solutions
In my table, I have a varchar column whereby multi-values are stored. An example
Let's have an example: using (var someObject = new SomeObject()) { var someOtherObject =
I saw this C# using statement in a code example: using StringFormat=System.Drawing.StringFormat; What's that
I'm trying to do a simple example using jQuerys fadeTo method. It works as
I have looked at this example using php and GD to piecewise-render a spiral

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.