Version 1 (modified by 18 years ago) ( diff ) | ,
---|
Inspired by 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:
- generated dot file: resulting image in PDF
- generated dot file: resulting image in PDF
I hope people might find it useful.
"""Django model to DOT (Graphviz) converter by Antonio Cavedoni <antonio@cavedoni.org> Make sure your DJANGO_SETTINGS_MODULE is set to your project and call the script like this: $ python modelviz.py <appname>.<models module_name> > <filename>.dot Example: $ python modelviz.py camera.models > foo.dot """ from django.db.models.base import ModelBase from django.db.models.fields.related import \ ForeignKey, OneToOneField, ManyToManyField def generate_dot(model_module): print "digraph model {" for obj in dir(model_module): o = getattr(model_module, obj) if isinstance(o, ModelBase): for field in o._meta.fields: if type(field) in [ForeignKey, OneToOneField, ManyToManyField]: print ' %s -> %s_%s [label="%s"];' % \ (o.__name__, o.__name__, field.name, type(field).__name__) print " %s_%s -> %s" % \ (o.__name__, field.name, field.rel.to.__name__) else: print " %s -> %s_%s;" % (o.__name__, o.__name__, field.name) print "}" if __name__ == "__main__": import sys _models = __import__(sys.argv[1]) generate_dot(_models.models)
Attachments (12)
-
camera-001.png
(12.0 KB
) - added by 18 years ago.
PNG version of camera-001.pdf
-
mincer-001.png
(30.4 KB
) - added by 18 years ago.
PNG version of mincer-001.pdf
-
mincer-modelviz-0.6.png
(102.3 KB
) - added by 18 years ago.
New modelviz sample image
-
modelviz.py
(3.5 KB
) - added by 17 years ago.
Updated script off of trunk @ Jun 6 2007
-
modelviz.2.py
(3.5 KB
) - added by 17 years ago.
Django Trunk GenericRelation Modify
-
modelviz.3.py
(5.4 KB
) - added by 17 years ago.
added other features.
-
modelviz.4.py
(5.6 KB
) - added by 17 years ago.
grays out blank=True fields
-
modelviz.5.py
(6.3 KB
) - added by 17 years ago.
-i, --include_models only include selected models in graph
-
modelviz.6.py
(8.1 KB
) - added by 17 years ago.
uploaded version from django-command-extensions
-
modelviz_i18n.py
(5.9 KB
) - added by 16 years ago.
Version with localized verbose_names instead of names
-
modelviz.7.py
(6.7 KB
) - added by 16 years ago.
Add exclude_models as well as include_models
-
modelviz.8.py
(7.5 KB
) - added by 16 years ago.
Added support for GeoDjango GeometryColumns and SpatialRefSys (requires fix in r9483)
Download all attachments as: .zip
Note:
See TracWiki
for help on using the wiki.