Post date: Jan 15, 2015 11:11:9 PM
Django is great because it allows one to build complex and highly-connected data models with minimal effort and high abstraction. Because of the possible complexity, however, keeping the data model straight can be hard without a visual representation. Luckily, with django-extensions, a python package, along with GraphViz, the python interface to GraphViz pydot, and a parser for pydot pyparsing, one can create a visual representation of a Django data models automatically.
First, install GraphVis, then pip install pyparsing==1.5.7, pydot and django-extensions, in that order. The current version of pydot in pypi has a significant windows bug, and will not be able to find GraphViz, so one must edit the pydot.py file installed to the python site-packages folder, editing line 401 on like so:
if os.sys.platform == 'win32':
try:
import win32api, win32con
# Get the GraphViz install path from the registry
#
try: # add this line and indent below
hkey = win32api.RegOpenKeyEx( win32con.HKEY_LOCAL_MACHINE,
"SOFTWARE\ATT\Graphviz", 0, win32con.KEY_QUERY_VALUE )
path = win32api.RegQueryValueEx( hkey, "InstallPath" )[0]
win32api.RegCloseKey( hkey )
print path
except: # add except with pass
pass
else: # add else and indent the following
# Now append the "bin" subdirectory:
#
path = os.path.join(path, "bin")
progs = __find_executables(path)
if progs is not None :
#print "Used Windows registry"
return progs
With the patch above, pydot will not read the non-existent registry key, but will search the path for the GraphViz bin folder. Be sure to add this folder to the path, as it is not added with the msi installer.
All of the above steps have already been completed on Basins. Thus, all one needs to do with a Django app on Basins add the requisite lines to the Django settings.py file, and then run a command. As for the lines to add to settings:
add 'django_extensions' to the INSTALLED_APPS tuple
add the following at the end of the file:
GRAPH_MODELS = {
'all_applications': True,
'group_models': True,
}
Now, to generate a PNG graphic of the models in the Django application, use this command: `python manage.py graph_models -a -g -o my_project_visualized.png`.