Created
October 27, 2011 18:17
-
-
Save dgouldin/1320358 to your computer and use it in GitHub Desktop.
Django model relation graph
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf import settings | |
from django.core.exceptions import ImproperlyConfigured | |
from django.db.models import loading | |
from django.db.models.fields.related import RelatedField | |
INDENT = ' ' | |
for app_name in settings.INSTALLED_APPS: | |
print app_name | |
app_label = app_name.split(".")[-1] | |
try: | |
app = loading.get_app(app_label) | |
except ImproperlyConfigured: | |
continue | |
for model in loading.get_models(app): | |
print INDENT*1 + '%s.%s' % (model._meta.app_label, model._meta.object_name) | |
for field in model._meta.fields: | |
if isinstance(field, RelatedField): | |
to = field.rel.to | |
print INDENT*2 + '%s -> %s.%s' % ( | |
field.name, | |
to._meta.app_label, | |
to._meta.object_name, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment