Is there a way of listing all python modules directly underneath a specified model in the hierarchy?
I’ve got a Django web-app that is slowly growing, and I’ve re-organised it based on this article:
http://paltman.com/2008/01/29/breaking-apart-models-in-django/
However, I’m trying to improve on his technique by making use of introspection in the module initialization file (__ init __.py) in order to auto-detect all instances of the Django model class in the subordinate jobs. I’ve got this sort of got this working, but it still needs a static list of modules in the tree above it to work.
In case people are interested, here’s what my solution looks like:
from django.db.models.base import ModelBase
from sys import modules
moduleList = ['TechTree', 'PilotAbilities']
__all__ = []
for moduleName in moduleList:
fullyQualifiedModuleName = '%s.%s' % (__name__, moduleName)
moduleObj = __import__(fullyQualifiedModuleName)
__all__ += [item for item in dir(moduleObj) if isinstance(getattr(moduleObj, item), ModelBase)]
Well, you can get
os.path.dirname(parent.__file__), and thenglob.glob()oros.walk()and look for other init.py files.