django: how to get list of apps that were not synced?

85 views Asked by At

when you execute bin/django syncdb a list with all apps that have been and those that haven't been synced gets returned.

Synced:
 > south
 > raven.contrib.django
 > django.contrib.staticfiles
     ...

Not synced (use migrations):
 - django_extensions
     ...

How can I intercept this information? I was looking at the post_syncdb signal but the call_back does not contain the info I was hoping for.

Edit Maybe I wasn't clear enough. I know I could do something like:

output = Popen(["bin/django","syncdb"], stdout=subprocess.PIPE).communicate()[0]

or:

def get_syncdb_output():
    content = StringIO()
    call_command('syncdb', stdout=content)
    import ipdb; ipdb.set_trace()
    content.seek(0)
    ansi_escape = re.compile(r'\x1b[^m]*m')
    return ansi_escape.sub('', content.read().decode('utf8'))

But I want to know in which django class or method this information is being produced!

1

There are 1 answers

1
bruno desthuilliers On

You know that Django is OSS, don't you ? The answer is written in plain text in the source code of django/core/management/commands/syncdb.py. For Django 1.6.5, its:

    db = options.get('database')
    connection = connections[db]
    cursor = connection.cursor()

    # Get a list of already installed *models* so that references work right.
    tables = connection.introspection.table_names()
    seen_models = connection.introspection.installed_models(tables)

For django 1.7.x and onward you'll have to look at django/core/management/commands/migrate.py. It's a bit more complex code but basically you'll want something like

    from django.db.migrations.executor import MigrationExecutor
    # Get the database we're operating from
    db = options.get('database')
    connection = connections[db]
    executor = MigrationExecutor(connection)
    print  executor.loader.unmigrated_apps