Changes between Initial Version and Version 1 of DjangoGraphviz


Ignore:
Timestamp:
Aug 4, 2006, 7:15:30 PM (18 years ago)
Author:
Antonio Cavedoni <antonio@…>
Comment:

Posted sample code, created page

Legend:

Unmodified
Added
Removed
Modified
  • DjangoGraphviz

    v1 v1  
     1Inspired by [http://www.hackdiary.com/archives/000093.html this nice hack by Matt Biddulph] I set up 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:
     2
     3 * [http://cavedoni.com/2006/08/camera.dot generated dot file]: resulting image in [http://cavedoni.com/2006/08/camera.pdf PDF]
     4 * [http://cavedoni.com/2006/08/mincer.dot generated dot file]: resulting image in [http://cavedoni.com/2006/08/mincer.pdf PDF]
     5
     6I hope people might find it useful.
     7
     8{{{
     9#!python
     10"""Django model to DOT (Graphviz) converter
     11by Antonio Cavedoni <antonio@cavedoni.org>
     12
     13Make sure your DJANGO_SETTINGS_MODULE is set to your project and call
     14the script like this:
     15
     16  $ python modelviz.py <appname>.<models module_name> > <filename>.dot
     17
     18Example:
     19
     20  $ python modelviz.py camera.models > foo.dot
     21"""
     22
     23from django.db.models.base import ModelBase
     24from django.db.models.fields.related import \
     25    ForeignKey, OneToOneField, ManyToManyField
     26
     27def generate_dot(model_module):
     28   print "digraph model {"
     29
     30   for obj in dir(model_module):
     31      o = getattr(model_module, obj)
     32      if isinstance(o, ModelBase):
     33         for field in o._meta.fields:
     34            if type(field) in [ForeignKey, OneToOneField, ManyToManyField]:
     35               print '  %s -> %s_%s [label="%s"];' % \
     36                   (o.__name__, o.__name__, field.name, type(field).__name__)
     37               print "  %s_%s -> %s" % \
     38                   (o.__name__, field.name, field.rel.to.__name__)
     39            else:
     40               print "  %s -> %s_%s;" % (o.__name__, o.__name__, field.name)
     41
     42   print "}"
     43
     44if __name__ == "__main__":
     45   import sys
     46   _models = __import__(sys.argv[1])
     47   generate_dot(_models.models)
     48}}}
Back to Top