Changes between Version 3 and Version 4 of DjangoGraphviz


Ignore:
Timestamp:
Aug 5, 2006, 4:42:40 AM (18 years ago)
Author:
Antonio Cavedoni <antonio@…>
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • DjangoGraphviz

    v3 v4  
    33Inspired 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:
    44
    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]
    77
    88I hope people might find it useful.
     
    1010{{{
    1111#!python
     12#!/usr/bin/python
    1213"""Django model to DOT (Graphviz) converter
    1314by Antonio Cavedoni <antonio@cavedoni.org>
     
    1617the script like this:
    1718
    18   $ python modelviz.py <appname>.<models module_name> > <filename>.dot
     19  $ python modelviz.py <app_label> > <filename>.dot
    1920
    2021Example:
    2122
    22   $ python modelviz.py camera.models > foo.dot
     23  $ python modelviz.py camera > foo.dot
    2324"""
    2425
    25 from django.db.models.base import ModelBase
     26from django.db import models
     27from django.db.models import get_models
    2628from django.db.models.fields.related import \
    2729    ForeignKey, OneToOneField, ManyToManyField
    2830
    29 def generate_dot(model_module):
    30    print "digraph model {"
     31def generate_dot(app_label):
     32   app = models.get_app(app_label)
    3133
    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)
    4380
    4481   print "}"
     
    4683if __name__ == "__main__":
    4784   import sys
    48    _models = __import__(sys.argv[1])
    49    generate_dot(_models.models)
     85   app_label = sys.argv[1]
     86   generate_dot(app_label)
    5087}}}
Back to Top