Index: django/core/management/commands/sqlall.py =================================================================== --- django/core/management/commands/sqlall.py (revision 11858) +++ django/core/management/commands/sqlall.py (working copy) @@ -1,10 +1,10 @@ -from django.core.management.base import AppCommand +from django.core.management.base import CrossAppCommand -class Command(AppCommand): +class Command(CrossAppCommand): help = "Prints the CREATE TABLE, custom SQL and CREATE INDEX SQL statements for the given model module name(s)." output_transaction = True - def handle_app(self, app, **options): + def handle_app(self, apps, **options): from django.core.management.sql import sql_all - return u'\n'.join(sql_all(app, self.style)).encode('utf-8') + return u'\n'.join(sql_all(apps, self.style)).encode('utf-8') Index: django/core/management/commands/sqlcustom.py =================================================================== --- django/core/management/commands/sqlcustom.py (revision 11858) +++ django/core/management/commands/sqlcustom.py (working copy) @@ -1,10 +1,10 @@ -from django.core.management.base import AppCommand +from django.core.management.base import CrossAppCommand -class Command(AppCommand): +class Command(CrossAppCommand): help = "Prints the custom table modifying SQL statements for the given app name(s)." output_transaction = True - def handle_app(self, app, **options): + def handle_app(self, apps, **options): from django.core.management.sql import sql_custom - return u'\n'.join(sql_custom(app, self.style)).encode('utf-8') + return u'\n'.join(sql_custom(apps, self.style)).encode('utf-8') Index: django/core/management/commands/sql.py =================================================================== --- django/core/management/commands/sql.py (revision 11858) +++ django/core/management/commands/sql.py (working copy) @@ -1,10 +1,10 @@ -from django.core.management.base import AppCommand +from django.core.management.base import CrossAppCommand -class Command(AppCommand): +class Command(CrossAppCommand): help = "Prints the CREATE TABLE SQL statements for the given app name(s)." output_transaction = True - def handle_app(self, app, **options): + def handle_app(self, apps, **options): from django.core.management.sql import sql_create - return u'\n'.join(sql_create(app, self.style)).encode('utf-8') + return u'\n'.join(sql_create(apps, self.style)).encode('utf-8') Index: django/core/management/commands/sqlreset.py =================================================================== --- django/core/management/commands/sqlreset.py (revision 11858) +++ django/core/management/commands/sqlreset.py (working copy) @@ -1,10 +1,10 @@ -from django.core.management.base import AppCommand +from django.core.management.base import CrossAppCommand -class Command(AppCommand): +class Command(CrossAppCommand): help = "Prints the DROP TABLE SQL, then the CREATE TABLE SQL, for the given app name(s)." output_transaction = True - def handle_app(self, app, **options): + def handle_app(self, apps, **options): from django.core.management.sql import sql_reset - return u'\n'.join(sql_reset(app, self.style)).encode('utf-8') + return u'\n'.join(sql_reset(apps, self.style)).encode('utf-8') Index: django/core/management/commands/sqlclear.py =================================================================== --- django/core/management/commands/sqlclear.py (revision 11858) +++ django/core/management/commands/sqlclear.py (working copy) @@ -1,10 +1,10 @@ -from django.core.management.base import AppCommand +from django.core.management.base import CrossAppCommand -class Command(AppCommand): +class Command(CrossAppCommand): help = "Prints the DROP TABLE SQL statements for the given app name(s)." output_transaction = True - def handle_app(self, app, **options): + def handle_app(self, apps, **options): from django.core.management.sql import sql_delete - return u'\n'.join(sql_delete(app, self.style)).encode('utf-8') + return u'\n'.join(sql_delete(apps, self.style)).encode('utf-8') Index: django/core/management/commands/sqlindexes.py =================================================================== --- django/core/management/commands/sqlindexes.py (revision 11858) +++ django/core/management/commands/sqlindexes.py (working copy) @@ -1,6 +1,6 @@ -from django.core.management.base import AppCommand +from django.core.management.base import CrossAppCommand -class Command(AppCommand): +class Command(CrossAppCommand): help = "Prints the CREATE INDEX SQL statements for the given model module name(s)." output_transaction = True Index: django/core/management/commands/reset.py =================================================================== --- django/core/management/commands/reset.py (revision 11858) +++ django/core/management/commands/reset.py (working copy) @@ -1,9 +1,9 @@ -from django.core.management.base import AppCommand, CommandError +from django.core.management.base import CrossAppCommand, CommandError from django.core.management.color import no_style from optparse import make_option -class Command(AppCommand): - option_list = AppCommand.option_list + ( +class Command(CrossAppCommand): + option_list = CrossAppCommand.option_list + ( make_option('--noinput', action='store_false', dest='interactive', default=True, help='Tells Django to NOT prompt the user for input of any kind.'), ) @@ -12,16 +12,17 @@ output_transaction = True - def handle_app(self, app, **options): + def handle_app(self, apps, **options): from django.db import connection, transaction from django.conf import settings from django.core.management.sql import sql_reset + + for app in apps: + app_name = app.__name__.split('.')[-2] - app_name = app.__name__.split('.')[-2] - self.style = no_style() - sql_list = sql_reset(app, self.style) + sql_list = sql_reset(apps, self.style) if options.get('interactive'): confirm = raw_input(""" Index: django/core/management/base.py =================================================================== --- django/core/management/base.py (revision 11858) +++ django/core/management/base.py (working copy) @@ -297,6 +297,33 @@ """ raise NotImplementedError() +class CrossAppCommand(BaseCommand): + """ + Modified version of AppCommand which allows for multiple applications to + see each other models. + """ + + args = '' + + def handle(self, *app_labels, **options): + from django.db import models + from django.core.management.sql import sql_create + if not app_labels: + raise CommandError('Enter at least one appname.') + try: + app_list = [models.get_app(app_label) for app_label in app_labels] + except (ImproperlyConfigured, ImportError), e: + raise CommandError("%s. Are you sure your INSTALLED_APPS setting is correct?" % e) + return self.handle_app(app_list, **options) + + def handle_app(self, apps, **options): + """ + Perform the command's actions for ``apps``, which will be the + Python module corresponding to an application name given on + the command line. + """ + raise NotImplementedError() + class LabelCommand(BaseCommand): """ A management command which takes one or more arbitrary arguments Index: django/core/management/sql.py =================================================================== --- django/core/management/sql.py (revision 11858) +++ django/core/management/sql.py (working copy) @@ -7,7 +7,7 @@ except NameError: from sets import Set as set # Python 2.3 fallback -def sql_create(app, style): +def sql_create(apps, style): "Returns a list of the CREATE TABLE SQL statements for the given app." from django.db import connection, models from django.conf import settings @@ -23,7 +23,13 @@ # We trim models from the current app so that the sqlreset command does not # generate invalid SQL (leaving models out of known_models is harmless, so # we can be conservative). - app_models = models.get_models(app, include_auto_created=True) + if isinstance(apps, list): + app_models=[] + for app in apps: + for mod in models.get_models(app, include_auto_created=True): + app_models.append(mod) + else: + app_models = models.get_models(apps, include_auto_created=True) final_output = [] tables = connection.introspection.table_names() known_models = set([model for model in connection.introspection.installed_models(tables) if model not in app_models]) @@ -54,7 +60,7 @@ return final_output -def sql_delete(app, style): +def sql_delete(apps, style): "Returns a list of the DROP TABLE SQL statements for the given app." from django.db import connection, models from django.db.backends.util import truncate_name @@ -76,22 +82,24 @@ # Output DROP TABLE statements for standard application tables. to_delete = set() - + if not isinstance(apps, list): + apps=[apps] references_to_delete = {} - app_models = models.get_models(app, include_auto_created=True) - for model in app_models: - if cursor and connection.introspection.table_name_converter(model._meta.db_table) in table_names: - # The table exists, so it needs to be dropped - opts = model._meta - for f in opts.local_fields: - if f.rel and f.rel.to not in to_delete: - references_to_delete.setdefault(f.rel.to, []).append( (model, f) ) + for app in apps: + app_models = models.get_models(app, include_auto_created=True) + for model in app_models: + if cursor and connection.introspection.table_name_converter(model._meta.db_table) in table_names: + # The table exists, so it needs to be dropped + opts = model._meta + for f in opts.local_fields: + if f.rel and f.rel.to not in to_delete: + references_to_delete.setdefault(f.rel.to, []).append( (model, f) ) - to_delete.add(model) + to_delete.add(model) - for model in app_models: - if connection.introspection.table_name_converter(model._meta.db_table) in table_names: - output.extend(connection.creation.sql_destroy_model(model, references_to_delete, style)) + for model in app_models: + if connection.introspection.table_name_converter(model._meta.db_table) in table_names: + output.extend(connection.creation.sql_destroy_model(model, references_to_delete, style)) # Close database connection explicitly, in case this output is being piped # directly into a database client, to avoid locking issues. @@ -120,25 +128,28 @@ statements = connection.ops.sql_flush(style, tables, connection.introspection.sequence_list()) return statements -def sql_custom(app, style): +def sql_custom(apps, style): "Returns a list of the custom table modifying SQL statements for the given app." from django.db.models import get_models output = [] - - app_models = get_models(app) - app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql')) - - for model in app_models: - output.extend(custom_sql_for_model(model, style)) - + app_models = [] + if not isinstance(apps, list): + apps=[apps] + for app in apps: + app_dir = os.path.normpath(os.path.join(os.path.dirname(app.__file__), 'sql')) + for model in get_models(app): + output.extend(custom_sql_for_model(model, style)) return output -def sql_indexes(app, style): +def sql_indexes(apps, style): "Returns a list of the CREATE INDEX SQL statements for all models in the given app." from django.db import connection, models output = [] - for model in models.get_models(app): - output.extend(connection.creation.sql_indexes_for_model(model, style)) + if not isinstance(apps, list): + apps=[apps] + for app in apps: + for model in models.get_models(app): + output.extend(connection.creation.sql_indexes_for_model(model, style)) return output def sql_all(app, style):