Ticket #7254: dumpdata.py.2.diff
File dumpdata.py.2.diff, 3.3 KB (added by , 16 years ago) |
---|
-
django/core/management/commands/dumpdata.py
2 2 from django.core import serializers 3 3 4 4 from optparse import make_option 5 import sys 5 6 6 7 class Command(BaseCommand): 7 8 option_list = BaseCommand.option_list + ( … … 9 10 help='Specifies the output serialization format for fixtures.'), 10 11 make_option('--indent', default=None, dest='indent', type='int', 11 12 help='Specifies the indent level to use when pretty-printing output'), 13 make_option('-e', '--exclude', dest='exclude',action='append', default=None, 14 help='App to exclude (use multiple --exclude to exclude multiple apps).'), 15 make_option('-v', '--verbosity', action='store', dest='verbosity', default='0', 16 type='choice', choices=['0', '1', '2'], 17 help='Verbosity level; 0=minimal output, 1=normal output, 2=all output'), 12 18 ) 13 19 help = 'Output the contents of the database as a fixture of the given format.' 14 20 args = '[appname ...]' … … 16 22 def handle(self, *app_labels, **options): 17 23 from django.db.models import get_app, get_apps, get_models 18 24 19 format = options.get('format', 'json') 20 indent = options.get('indent', None) 21 show_traceback = options.get('traceback', False) 25 format = options.get('format') 26 indent = options.get('indent') 27 exclude = options.get('exclude') 28 verbosity = int(options.get('verbosity')) 29 show_traceback = options.get('traceback') 22 30 31 if verbosity >= 2: 32 sys.stderr.write( 33 "format %s\nindent %s\nexclude %s\nshow_traceback %s\n" % \ 34 (format, indent, exclude, show_traceback) ) 35 sys.stderr.write( "apps %s\n" % (app_labels,) ) 36 23 37 if len(app_labels) == 0: 24 38 app_list = get_apps() 25 39 else: 26 40 app_list = [get_app(app_label) for app_label in app_labels] 27 41 42 if exclude: 43 excl_apps = [get_app(app_label) for app_label in exclude] 44 for excl_app in excl_apps: 45 app_list.remove(excl_app) 46 if verbosity >= 2: 47 sys.stderr.write( "excluded: %s\n" % excl_app.__name__ ) 48 49 if verbosity >= 2: 50 for app in app_list: 51 sys.stderr.write( "included: %s\n" % app.__name__ ) 52 28 53 # Check that the serialization format exists; this is a shortcut to 29 54 # avoid collating all the objects and _then_ failing. 30 55 if format not in serializers.get_public_serializer_formats(): … … 37 62 38 63 objects = [] 39 64 for app in app_list: 65 if verbosity >= 1: 66 sys.stderr.write( "dumping %s/n" % (app.__name__) ) 40 67 for model in get_models(app): 68 if verbosity >= 1: 69 # model dosn't have a good __str, 70 # so for now "<class... model_name>" is better than nothing. 71 sys.stderr.write("dumping %s,%s\n" % (app.__name__, model)) 41 72 objects.extend(model._default_manager.all()) 42 73 try: 43 74 return serializers.serialize(format, objects, indent=indent)