Automatic graph of your database structure

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python

# Copyright (C) 2008 Adam Lofts
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# This program generates a graphviz file to plot a graph of your database layout.
# 
# Usage:
#
# Set PYTHONPATH and DJANGO_SETTINGS_MODULE for your django app
# ./graph.py <django_app_name> | dot -Tpng -o graph.png

import sys
from django.db.models import get_app, get_models

def graph(app, outfile):

    models = get_models(get_app(app))

    outfile.write("digraph G {\n")

    for model in models:

        # Format the shape
        name = model._meta.object_name
        label = "%s\\n" % name + "\\n".join([field.name for field in model._meta._fields()])
        outfile.write("%s [shape=box,label=\"%s\"];" % (name, label))

        # Draw the relations
        for related in model._meta.get_all_related_objects():
            outfile.write("\t%s -> %s;\n" % (name, related.model._meta.object_name))

        for related in model._meta.get_all_related_many_to_many_objects():
            outfile.write("\t%s -> %s [dir=both];\n" % (name, related.model._meta.object_name))

    outfile.write("}\n")

if __name__=="__main__":

    if len(sys.argv) != 2:
        print "graph.py <appname>"
        print "\tWrites a graph of your models suitable for processing with graphviz"
        sys.exit(1)

    graph(sys.argv[1], sys.stdout)

More like this

  1. Diagram of your database structure by vnbang2003 2 years, 8 months ago
  2. Model dependency graph using graphviz (script) by andrew 5 years, 7 months ago
  3. dynamic model graph by findlay 5 years, 11 months ago
  4. Simple Entity Relationship Diagram using GraphViz by bendavis78 4 years, 11 months ago
  5. Unit Test Profiling for Django 1.3/1.4 by hoffmaje 2 years ago

Comments

(Forgotten your password?)