Django Models and Inherited Class Integration

If I have existing classes (generated from some UML model) from legacy code, what is the best way to integrate them with Django class classes ?

I have so far considered using custom Django fields to serialize a class and letting this handle persistence. (The disadvantage of this is that other applications accessing the database directly - if it ever came to this requirement - had to deserialize this field in order to access the data.)

If there is anyone who can offer some alternatives to the above - such that the persistence around my existing classes can be "replaced" would be very helpful!

+5
source share
1 answer

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:

script 3:

  # legacy/management/commands/importlegacydb.py

  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."
+2

All Articles