Changes between Version 7 and Version 8 of DjangoGraphviz


Ignore:
Timestamp:
08/06/06 06:50:36 (9 years ago)
Author:
Antonio Cavedoni <antonio@…>
Comment:

Added model viz for the Zyons project, edited code to support GenericRelations and OneToOneFields

Legend:

Unmodified
Added
Removed
Modified
  • DjangoGraphviz

    v7 v8  
    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-001.dot generated dot file]: resulting image in [http://cavedoni.com/2006/08/camera-001.pdf PDF],
     5 * Graphviz DOT files for the Zyons project: [http://cavedoni.com/2006/08/zyons-comment.dot comment] [http://cavedoni.com/2006/08/zyons-event.dot event] [http://cavedoni.com/2006/08/zyons-forum.dot forum] [http://cavedoni.com/2006/08/zyons-openid.dot openid] [http://cavedoni.com/2006/08/zyons-prefs.dot prefs] [http://cavedoni.com/2006/08/zyons-profile.dot profile] [http://cavedoni.com/2006/08/zyons-sessions.dot sessions] [http://cavedoni.com/2006/08/zyons-tag.dot tag]
     6 * Resulting graphs in PDF: [http://cavedoni.com/2006/08/zyons-comment.pdf comment] [http://cavedoni.com/2006/08/zyons-event.pdf event] [http://cavedoni.com/2006/08/zyons-forum.pdf forum] [http://cavedoni.com/2006/08/zyons-openid.pdf openid] [http://cavedoni.com/2006/08/zyons-prefs.pdf prefs] [http://cavedoni.com/2006/08/zyons-profile.pdf profile] [http://cavedoni.com/2006/08/zyons-sessions.pdf sessions] [http://cavedoni.com/2006/08/zyons-tag.pdf tag]
     7
     8 * Other samples:
     9   * [http://cavedoni.com/2006/08/camera-001.dot generated dot file]: resulting image in [http://cavedoni.com/2006/08/camera-001.pdf PDF],
    610{{{
    711#!html
    812<a href="http://code.djangoproject.com/attachment/wiki/DjangoGraphviz/camera-001.png">PNG</a>
    913}}}
    10  * [http://cavedoni.com/2006/08/mincer-001.dot generated dot file]: resulting image in [http://cavedoni.com/2006/08/mincer-001.pdf PDF],
     14   * [http://cavedoni.com/2006/08/mincer-001.dot generated dot file]: resulting image in [http://cavedoni.com/2006/08/mincer-001.pdf PDF],
    1115{{{
    1216#!html
     
    3640from django.db.models.fields.related import \
    3741    ForeignKey, OneToOneField, ManyToManyField
     42from django.db.models.fields.generic import GenericRelation
    3843
    3944def generate_dot(app_label):
     
    5762      nodes = []
    5863      nodes.append("""  %s [
    59     label = "{%s | """ % (o.__name__, o.__name__))
     64    label = "{%s |""" % (o.__name__, o.__name__))
    6065
    6166      # members
    6267      for field in o._meta.fields:
    63          nodes.append("%s : %s\\l" % (field.name, type(field).__name__))
     68         nodes.append('%s : %s\\l' % (field.name, type(field).__name__))
    6469         
    65       for field in o._meta.many_to_many:
    66          nodes.append("%s : %s\\l" % (field.name, type(field).__name__))
     70      if o._meta.many_to_many:
     71         for field in o._meta.many_to_many:
     72            nodes.append('%s : %s\\l' % (field.name, type(field).__name__))
     73
     74      if o._meta.one_to_one_field:
     75         for field in o._meta.one_to_one_field:
     76            nodes.append('%s : %s\\l' % (field.name, type(field).__name__))
    6777
    6878      nodes.append(""" }"\n]\n""")
     
    7383      for field in o._meta.fields:
    7484         if isinstance(field, ForeignKey):
    75             _rel = '  %s -> %s [label="%s"];' % \
     85            _rel = ' %s -> %s [label="%s"];' % \
    7686                (o.__name__, field.rel.to.__name__, type(field).__name__)
    7787            if _rel not in rel:
    7888               rel.append(_rel)
    7989
    80       for field in o._meta.many_to_many:
    81          if isinstance(field, ManyToManyField):
    82             _rel = '  %s -> %s [label="%s"] [arrowhead=normal arrowtail=normal];' % \
    83                 (o.__name__, field.rel.to.__name__, type(field).__name__)
    84             if _rel not in rel:
    85                rel.append(_rel)
     90      if o._meta.many_to_many:
     91         for field in o._meta.many_to_many:
     92            if isinstance(field, ManyToManyField):
     93               _rel = '  %s -> %s [label="%s"] [arrowhead=normal arrowtail=normal];' % \
     94                   (o.__name__, field.rel.to.__name__, type(field).__name__)
     95               if _rel not in rel:
     96                  rel.append(_rel)
     97
     98            if isinstance(field, GenericRelation):
     99               _rel = '  %s -> %s [label="%s"] [style="dotted"] [arrowhead=normal arrowtail=normal];' % \
     100                   (o.__name__, field.rel.to.__name__, type(field).__name__)
     101               if _rel not in rel:
     102                  rel.append(_rel)
     103
     104      if o._meta.one_to_one_field:
     105         for field in o._meta.one_to_one_field:
     106            if isinstance(field, OneToOneField):
     107               _rel = '  %s -> %s [label="%s"] [arrowhead=none arrowtail=none];' % \
     108                   (o.__name__, field.rel.to.__name__, type(field).__name__)
     109               if _rel not in rel:
     110                  rel.append(_rel)
    86111
    87112      print "\n".join(rel)
Back to Top