Version 11 (modified by Antonio Cavedoni, 9 years ago) (diff)

Code refactoring

Converting Django models into Graphviz DOT files

Inspired by 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:

You might also be interested in this Django app by Andrew Barilla from which I borrowed some ideas, that displays the graphviz results directly from the web.

"""Django model to DOT (Graphviz) converter
by Antonio Cavedoni <>

Make sure your DJANGO_SETTINGS_MODULE is set to your project and call 
the script like this:

  $ python <app_label> > <filename>.dot


Cleaned up code, now the relationship arrows start from the 
correct model attribute position

Fixed OneToOneField support (thanks, limodou)

Added support for GenericRelation and OneToOneField

Changed display (shape="record", thanks Malcolm Tredinnick), 
fixed arrow direction, added display of model attributes 
(thanks, Russell Keith-Magee)

First release.

__version__ = "0.4"
__svnid__ = "$Id: 4 2006-08-06 19:48:42Z verbosus $"
__license__ = "Python"
__author__ = "Antonio Cavedoni <>"
__contributors__ = [
   "Stefano J. Attardi <>",
   "limodou <>"

from django.db import models
from django.db.models import get_models
from django.db.models.fields.related import \
    ForeignKey, OneToOneField, ManyToManyField
from django.db.models.fields.generic import GenericRelation

def generate_dot(app_label):
   app = models.get_app(app_label)

   print "digraph %s {" % ('"' + app.__name__ + '"')
   print """  fontname = "Helvetica"
  fontsize = 8
  node [
    fontname = "Helvetica"
    fontsize = 8
    shape = "record"
   edge [
    fontname = "Helvetica"
    fontsize = 8
   for o in get_models(app):
      nodes = []
      nodes.append(""" subgraph cluster_%(model)s {
  shape = "record";
  label = "%(model)s";
  fontname = "Helvetica Bold";
  fontsize = 10;
  labelfontcolor = "black";
  %(model)s [label = "{""" % {'model': o.__name__})

      # model attributes
      def add_attributes():
         nodes.append("<%(model)s_%(field)s>%(field)s : %(related)s|" % \
                      {'model': o.__name__, 'field':, 
                       'related': type(field).__name__})

      for field in o._meta.fields:
      if o._meta.many_to_many:
         for field in o._meta.many_to_many:

      nodes.append(""" }"] [color="white" shape="record"];\n }\n""")
      print "".join(nodes)

      # relations
      rel = []

      def add_relation(extras=""):
         _rel = """ %(model)s:%(model)s_%(field)s -> %(related)s 
         [label="%(relationship)s"] %(extras)s;""" % {
            'model': o.__name__, 'field':, 
            'relationship': type(field).__name__,
            'extras': extras}
         if _rel not in rel:

      for field in o._meta.fields:
         if isinstance(field, ForeignKey):
         elif isinstance(field, OneToOneField):
            add_relation("[arrowhead=none arrowtail=none]")

      if o._meta.many_to_many:
         for field in o._meta.many_to_many:
            if isinstance(field, ManyToManyField):
               add_relation("[arrowhead=normal arrowtail=normal]")
            elif isinstance(field, GenericRelation):
                  '[style="dotted"] [arrowhead=normal arrowtail=normal]')

      print "\n".join(rel)

   print "}"

if __name__ == "__main__":
   import sys
   app_label = sys.argv[1]

Attachments (12)

Download all attachments as: .zip

Back to Top