#!/usr/bin/env python # -*- coding: utf-8 -*- """ Warning: This python script is designed for Django 0.96. It exports data from models quite like the `dumpdata` command, and throws the data to the standard output. It fixes glitches with unicode/ascii characters. It looked like the 0.96 handles very badly unicode characters, unless you specify an argument that is not available via the command line. The simple usage is: $ python export_models.py -a [application2, application3...] As a plus, it allows you to export only one or several models inside your application, and not all of them: $ python export_models.py application1.MyModelStuff application1.MyOtherModel Of course, you can specify the output format (serializer) with the -f (--format) option. $ python export_models.py --format=xml application1.MyModel """ import sys import os from optparse import OptionParser # You first need to upgrade your python path SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__)) sys.path = [os.path.join(SCRIPT_DIR, '../')] + sys.path # Django os.environ['DJANGO_SETTINGS_MODULE'] = '#MYPROJECT#.settings' project_name, settings_name = os.environ['DJANGO_SETTINGS_MODULE'].split('.') if not os.path.isdir(os.path.join(SCRIPT_DIR, '../', project_name)): print """ERROR! You need to check and fix the os.environ['DJANGO_SETTINGS_MODULE'] variable. It looks like the project path is wrong.""" sys.exit(1) if not os.path.isfile(os.path.join(SCRIPT_DIR, settings_name + ".py")): print """ERROR! You need to check and fix the os.environ['DJANGO_SETTINGS_MODULE'] variable. It looks like the settings file does not exists""" sys.exit(1) from django.conf import settings # IMPORT SECTION -------------------------------------------------------------- try: from MYPROJECT.models import * # Here you can import other modules from your Django app except ImportError: print """ERROR! It looks like you need to fix your module imports in the script. Please edit it and check the import section""" sys.exit(1) # END IMPORT SECTION ---------------------------------------------------------- from django.core import serializers from django.db.models import get_app, get_apps, get_models, get_model def get_options(): "defines options and arguments" usage = """usage: %prog [-d] [-f format] app1.model1 app2.model2... or %prog -a app1 app2 """ parser = OptionParser(usage) parser.add_option("-f", "--format", dest="format", action="store", type="string", default="json", help="format may be 'yaml' or 'json'. Default is 'json'") parser.add_option("-d", "--debug", dest="debug", action="store_true", default=False, help="prints debug information") parser.add_option("-a", "--all", dest="all", action="store_true", default=False, help='serialises every model available in the given applications' ) return parser.parse_args() def main(): "main program" options, args = get_options() JSONSerializer = serializers.get_serializer("json") objects = [] app_labels = [] if options.all: if options.debug: print "All" for application in args: for model in get_models(get_app(application)): app_labels.append(tuple([application, model.__name__])) else: app_labels = [tuple(m.split('.', 1)) for m in args] if options.debug: print app_labels for app, model_name in app_labels: modele = get_model(app, model_name) if options.debug: print modele, app, model_name if modele: objects.extend(modele._default_manager.all()) if options.format == 'json': data = serializers.serialize(options.format, objects, ensure_ascii=False, indent=4) else: data = serializers.serialize(options.format, objects, indent=4) print data if __name__ == '__main__': main() # EOF