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 <application1> [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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | #!/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 <application1> [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
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
Please login first before commenting.