Created
October 26, 2012 02:29
-
-
Save AaronGhent/3956578 to your computer and use it in GitHub Desktop.
Sync Django Models Properly
This file contains 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
# >><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><< # | |
# Sync's models against database | |
# >><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><< # | |
from cStringIO import StringIO | |
import sys | |
import re | |
import os | |
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings") | |
output = StringIO() | |
old_stdout = sys.stdout | |
sys.stdout = output | |
from django.core import management | |
management.call_command('inspectdb', verbosity=0, interactive=False) | |
sys.stdout = old_stdout | |
def sandr(m): | |
matched = m.group(1) | |
return "models.ForeignKey('" + matched + "'" | |
blah = re.sub('models.ForeignKey\((\w+)', sandr, output.getvalue(), flags=re.M) | |
print blah | |
# >><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><<>><< # |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment