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

  • SEARCH
  • Home
  • 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 7612685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:58:34+00:00 2026-05-31T01:58:34+00:00

For a Django tab library, I created an architecture that uses tracking of marked

  • 0

For a Django tab library, I created an architecture that uses tracking of marked subclasses.

To do this, I created a base class and then derive all tab classes from it. I track the descendant classes using a function that recursively uses the cls.__subclasses__() method.

In order to know which subclasses are leaf classes / real tabs, I chose to do this the manual way by adding __tab__ = True to any class I want to show up as a tab. The reason for this is that I’m creating other abstraction classes below TabView that shouldn’t show up as tabs. Maybe this could be re-written as a decorator.

Example:

def get_descendants(cls):
    """Returns all subclasses for cls, and their sublasses, and so on..."""
    descendants = []
    subclasses = cls.__subclasses__()
    for subclass in subclasses:
        descendants.append(subclass)
        descendants += get_descendants(subclass)
    return descendants

def TabView(object):
    def _tab_group_members(self):
        descendants = get_descendants(TabView)
        return [d for d in descendants if '__tab__' in d.__dict__]
    (...)

def ConcreteTab(TabView):
    __tab__ = True

Now I started reading Marty Alchin’s “Pro Django” book. In there, he proposes the usage of metaclasses to track subclasses:

class SubclassTracker(type):
    def __init__(cls, name, bases, attrs):
        try:
            if TrackedClass not in bases:
                return
        except NameError:
            return
        TrackedClass._registry.append(cls)

class TrackedClass(object):
    __metaclass__ = SubclassTracker
    _registry = []

What are the advantages of the metaclass approach? Is it better than using __subclasses__()?

  • 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-05-31T01:58:36+00:00Added an answer on May 31, 2026 at 1:58 am

    Using metaclasses means that all the information you stored inside the class itself. It also means the info is stored as the classes are created, rather than scanning all of them afterwards. This means that all you need to worry about is creating classes – they are tracked automatically.

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

Sidebar

Related Questions

Django uses Meta class for lots of additional object information. However, they store this
Django has the concept of filters that allow you to format values in views.
I have tabs that calls via javascript urls of django to complete the container
I have a Django app deployed on Google App Engine that prematurely logs its
Django Newbie question. I have the following model: class Leg(models.Model): drive_date = models.DateField() startpoint
Django has a DATE_FORMAT and a DATE_TIME_FORMAT options that allow us to choose which
I was able to successfully render my first django form inside an extjs tab.
Django docs say that Context object is a stack: from django.template import Context c
Django auth User model has a date_joined DateTimeField . Could this be used to
I have a written views in django where i have more than one tab

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.