Login

Simple snippet to generate whole project models uml diagram in YUML.me format

Author:
nikolajusk
Posted:
October 6, 2010
Language:
Python
Version:
Not specified
Tags:
django command basecommand uml yuml
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

  1. A small script to rearranging the models generated from inspectdb command by m_tayseer 6 years, 8 months ago
  2. Copy/Paste form generation by dballanc 7 years, 8 months ago
  3. Generating Taconite command documents by desfrenes 7 years, 2 months ago
  4. remove the annoying "Hold down control..." messages by arthur 4 years, 4 months ago
  5. Automatically generate admin by WoLpH 2 years, 7 months ago

Comments

Please login first before commenting.