This view assumes you have downloaded modelviz.py and placed it in a python module called utils within my_project. You also must have graphviz installed and a subprocess-capable python. From there you can feed it a URL query list of the options you want to pass to generate_dot, and it will dynamically draw png or svg images of your model relationships right in the browser. All it wants is a nice form template for graphically selecting models. Most of this code and the main idea thereof was shamelessly plagiarized from someone else.
Examples:
http://localhost:8000/model_graph/?image_type=svg&app_labels=my_app&include_models=Polls&include_models=Choices
http://localhost:8000/model_graph/?image_type=png&all_applications&disable_fields&group_models
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 | urls.py ----------
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^model_graph/$','<my_project>.<my_app>.views.model_graph'),
)
<my_app>/views.py ----------
from django.http import HttpResponse
from subprocess import Popen,PIPE
from <my_project>.utils.modelviz import generate_dot
def model_graph(request):
options = dict((str(key),val) for key,val in request.GET.iteritems())
if 'app_labels' in options:
app_labels = options['app_labels']
del options['app_labels']
if 'image_type' in options:
image_type = options['image_type'][0]
del options['image_type']
dot = Popen(['dot','-T%s' % image_type],stdin=PIPE,stdout=PIPE)
content = generate_dot(app_labels,**options)
if image_type == 'svg' : image_type = 'svg+xml'
response = HttpResponse(mimetype='image/%s' % image_type)
dot.stdin.write(content)
dot.stdin.close()
response.write(dot.stdout.read())
dot.stdout.close()
return response
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.