If you are trying to move an obsolete application to django, I agree with @chrisdpratt and you should try to convert your classes to Django models. It will take a lot of effort, so when you are ready, you can follow this trail:
Build the application legacyand place the old code there.
, , , SQL-, inspectdb, " ", . DB, "legacy". . https://docs.djangoproject.com/en/dev/topics/db/multi-db/
script, , . ( / DJANGO_SETTINGS_MODULE . https://docs.djangoproject.com/en/dev/ref/django-admin/?from=olddocs#running-management-commands-from-your-code).
django ( "myapp" ).
inspectdb, db. , Django .
script, .
.
script 3:
from django.core.management.base import NoArgsCommand
import myapp.models as M
import legacy.models as L
import sys
write = sys.stdout.write
def copy_fields(old, new, mapping):
for old_key, new_key in mapping.items():
value = getattr(old, old_key)
if type(value) is str:
value = value.strip()
if type(new_key) is tuple:
value = new_key[0](value)
new_key = new_key[1]
else:
if new_key == "name":
value = value[0].upper() + value[1:]
setattr(new, new_key, value)
def import_table(old_class, new_class, mapping):
write("importing %s " % old_class.__name__)
lookup = {}
l = old_class.objects.all()
for old in l:
new = new_class()
copy_fields(old, new, mapping)
new.save()
lookup[old.id] = new.id
write (".")
print " Done."
return lookup
class Command(NoArgsCommand):
help = "Import data from legacy db."
def handle_noargs(self, **options):
"""
Read data from legacy db to new db.
"""
print "Importing legacy data"
import_table(L.X, M.X, { 'old_field' : 'new_field', 'old_field2' : 'new_field2'})
import_table(L.Y, M.Y, { 'old_field' : 'new_field'})
print "Done."