- Author:
- nikolajusk
- Posted:
- October 6, 2010
- Language:
- Python
- Version:
- Not specified
- Score:
- 0 (after 0 ratings)
launch python manage.py yourcommand and command output paste into http://www.yuml.me/diagram/scruffy/class/draw
todo: 1.implement other relation attributes 2.add class inheritance 3.download image automatically 4.generate diagram for specific app
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 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])
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.