Changes between Version 7 and Version 8 of DjangoGraphviz
- Timestamp:
- Aug 6, 2006, 6:50:36 AM (18 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
DjangoGraphviz
v7 v8 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-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], 6 10 {{{ 7 11 #!html 8 12 <a href="http://code.djangoproject.com/attachment/wiki/DjangoGraphviz/camera-001.png">PNG</a> 9 13 }}} 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], 11 15 {{{ 12 16 #!html … … 36 40 from django.db.models.fields.related import \ 37 41 ForeignKey, OneToOneField, ManyToManyField 42 from django.db.models.fields.generic import GenericRelation 38 43 39 44 def generate_dot(app_label): … … 57 62 nodes = [] 58 63 nodes.append(""" %s [ 59 label = "{%s | 64 label = "{%s |""" % (o.__name__, o.__name__)) 60 65 61 66 # members 62 67 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__)) 64 69 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__)) 67 77 68 78 nodes.append(""" }"\n]\n""") … … 73 83 for field in o._meta.fields: 74 84 if isinstance(field, ForeignKey): 75 _rel = ' 85 _rel = ' %s -> %s [label="%s"];' % \ 76 86 (o.__name__, field.rel.to.__name__, type(field).__name__) 77 87 if _rel not in rel: 78 88 rel.append(_rel) 79 89 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) 86 111 87 112 print "\n".join(rel)