from django.core.management.base import BaseCommand, CommandError from django.db.models.loading import get_models, get_apps class Command(BaseCommand): args = '<...>' help = 'Generating model class diagram for yuml.me' def handle(self, *args, **options): loaded_models = get_models() imported_objects = {} for app_mod in get_apps(): app_models = get_models(app_mod) if not app_models: continue model_labels = ", ".join([model.__name__ for model in app_models]) for model in app_models: try: imported_objects[model.__name__] = getattr(__import__(app_mod.__name__, {}, {}, model.__name__), model.__name__) except AttributeError, e: continue for model_name in imported_objects: model = imported_objects[model_name] obj_str = "" obj_str = "[%s|" % (model._meta.module_name) relations = {} for field in model._meta.fields: if field.__class__.__name__=='ForeignKey': relations[field.attname] = field.related.parent_model._meta.module_name obj_str += "%s (%s);" % (field.attname, field.__class__.__name__) obj_str+="]" print obj_str if len(relations)>0: for column in relations: print "[%s]-%s>[%s]"% (model._meta.module_name,column,relations[column])