Changes between Version 3 and Version 4 of DjangoGraphviz
- Timestamp:
- Aug 5, 2006, 4:42:40 AM (18 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
DjangoGraphviz
v3 v4 3 3 Inspired by [http://www.hackdiary.com/archives/000093.html this nice hack by Matt Biddulph] I set out to create a similar thing for Django models. Below is an initial implementation, and here are some results generated with the OS X version of Graphviz: 4 4 5 * [http://cavedoni.com/2006/08/camera .dot generated dot file]: resulting image in [http://cavedoni.com/2006/08/camera.pdf PDF]6 * [http://cavedoni.com/2006/08/mincer .dot generated dot file]: resulting image in [http://cavedoni.com/2006/08/mincer.pdf PDF]5 * [http://cavedoni.com/2006/08/camera-001.dot generated dot file]: resulting image in [http://cavedoni.com/2006/08/camera-001.pdf PDF] 6 * [http://cavedoni.com/2006/08/mincer-001.dot generated dot file]: resulting image in [http://cavedoni.com/2006/08/mincer-001.pdf PDF] 7 7 8 8 I hope people might find it useful. … … 10 10 {{{ 11 11 #!python 12 #!/usr/bin/python 12 13 """Django model to DOT (Graphviz) converter 13 14 by Antonio Cavedoni <antonio@cavedoni.org> … … 16 17 the script like this: 17 18 18 $ python modelviz.py <app name>.<models module_name> > <filename>.dot19 $ python modelviz.py <app_label> > <filename>.dot 19 20 20 21 Example: 21 22 22 $ python modelviz.py camera .models> foo.dot23 $ python modelviz.py camera > foo.dot 23 24 """ 24 25 25 from django.db.models.base import ModelBase 26 from django.db import models 27 from django.db.models import get_models 26 28 from django.db.models.fields.related import \ 27 29 ForeignKey, OneToOneField, ManyToManyField 28 30 29 def generate_dot( model_module):30 print "digraph model {"31 def generate_dot(app_label): 32 app = models.get_app(app_label) 31 33 32 for obj in dir(model_module): 33 o = getattr(model_module, obj) 34 if isinstance(o, ModelBase): 35 for field in o._meta.fields: 36 if type(field) in [ForeignKey, OneToOneField, ManyToManyField]: 37 print ' %s -> %s_%s [label="%s"];' % \ 38 (o.__name__, o.__name__, field.name, type(field).__name__) 39 print " %s_%s -> %s" % \ 40 (o.__name__, field.name, field.rel.to.__name__) 41 else: 42 print " %s -> %s_%s;" % (o.__name__, o.__name__, field.name) 34 print "digraph %s {" % ('"' + app.__name__ + '"') 35 print """ fontname = "Helvetica" 36 fontsize = 8 37 38 node [ 39 fontname = "Helvetica" 40 fontsize = 8 41 shape = "record" 42 ] 43 edge [ 44 fontname = "Helvetica" 45 fontsize = 8 46 ] 47 """ 48 for o in get_models(app): 49 nodes = [] 50 nodes.append(""" %s [ 51 label = "{%s | """ % (o.__name__, o.__name__)) 52 53 # members 54 for field in o._meta.fields: 55 nodes.append("%s : %s\\l" % (field.name, type(field).__name__)) 56 57 for field in o._meta.many_to_many: 58 nodes.append("%s : %s\\l" % (field.name, type(field).__name__)) 59 60 nodes.append(""" }"\n]\n""") 61 print "".join(nodes) 62 63 # relations 64 rel = [] 65 for field in o._meta.fields: 66 if isinstance(field, ForeignKey): 67 _rel = ' %s -> %s [label="%s"];' % \ 68 (o.__name__, field.rel.to.__name__, type(field).__name__) 69 if _rel not in rel: 70 rel.append(_rel) 71 72 for field in o._meta.many_to_many: 73 if isinstance(field, ManyToManyField): 74 _rel = ' %s -> %s [label="%s"] [arrowhead=normal arrowtail=normal];' % \ 75 (o.__name__, field.rel.to.__name__, type(field).__name__) 76 if _rel not in rel: 77 rel.append(_rel) 78 79 print "\n".join(rel) 43 80 44 81 print "}" … … 46 83 if __name__ == "__main__": 47 84 import sys 48 _models = __import__(sys.argv[1])49 generate_dot( _models.models)85 app_label = sys.argv[1] 86 generate_dot(app_label) 50 87 }}}