Skip to content

Instantly share code, notes, and snippets.

@EyePulp
Last active December 14, 2015 04:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EyePulp/5027698 to your computer and use it in GitHub Desktop.
Save EyePulp/5027698 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# don't forget each model needs
# class Meta:
# app_label = 'app_name' # or whatever the app name is
PACKAGE = 'project_name.app_name.models' # <--- SET YOUR PACKAGE NAME
import os
import types
module_methods = []
model_dir = os.path.dirname(__file__)
for name in os.listdir(model_dir): # Search through every file inside this directory.
if os.path.isfile(os.path.join(model_dir,name)) and name.lower().endswith('.py') and name != "__init__.py":
mname = name.rpartition('.')[0]
module = __import__('%s.%s' % (PACKAGE, mname),{}, {},mname) # Import the module file
for method in dir(module): # find all clases inside it.
item = getattr(module, method)
if isinstance(item, (type, types.ClassType)):
exec "%s = item" % method # Found a class, bring into the module namespace.
module_methods.append(method)
__all__ = module_methods # Hide everything other than the classes from other modules.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment