well I'll be damned, it worked

usage:

apt = Apt()
# old is coming via multi-db connection to mysql
# apt is saved in postgres
apt.id = old.id

# the seq name is {app}_{model}_id_seq

legacy_save(apt,'nsproperties_apt_id_seq')

# after all apts are imported then set the seq correctly:
set_max_seq(Apt,'nsproperties_apt_id_seq')


def legacy_save(self, seqname):
    """
     forces an insert on a model 'self' even though the pk is set.
     for one-time insertion of objects while preserving the pk from a
legacy schema
    """
    from django.db import connections, transaction
    raw = False
    using = 'default'
    connection = connections[using]
    cls = self.__class__
    meta = cls._meta

    # If we are in a raw save, save the object exactly as presented.
    # That means that we don't try to be smart about saving attributes
    # that might have come from the parent class - we just save the
    # attributes we have been given to the class we have been given.
    # We also go through this process to defer the save of proxy
objects
    # to their actual underlying model.
    if not raw or meta.proxy:
        if meta.proxy:
            org = cls
        else:
            org = None
        for parent, field in meta.parents.items():
            # At this point, parent's primary key field may be unknown
            # (for example, from administration form which doesn't
fill
            # this field). If so, fill it.
            if field and getattr(self, parent._meta.pk.attname) is
None and getattr(self, field.attname) is not None:
                setattr(self, parent._meta.pk.attname, getattr(self,
field.attname))

            self.save_base(cls=parent, origin=org, using=using)

            if field:
                setattr(self, field.attname,
self._get_pk_val(parent._meta))
        if meta.proxy:
            return

    if not meta.proxy:
        manager = cls._base_manager
        values = [(f, f.get_db_prep_save(raw and getattr(self,
f.attname) or f.pre_save(self, True), connection=connection))
            for f in meta.local_fields]

        update_pk = True

        cursor = connection.cursor() #...@undefinedvariable

        # somewhat of a hack, to keep postgres quiet about the seq not
being selected for this session
        # its actually saying "the last object saved had this id"
        # although it will really be the one we are about to save
        cursor.execute("SELECT setval('%s'::regclass, %s);" %
(seqname,self.pk))
        # Create a new record.
        result = manager._insert(values, return_id=update_pk,
using=using)

        assert self.pk == result,Exception("saved pk is not self pk")

        transaction.commit_unless_managed(using=using)

    # Store the database on which the object was saved
    self._state.db = using


def set_max_seq(model,seqname):
    max = model.objects.order_by('-id')[0]
    max_id = max.pk + 1
    cursor = connection.cursor() #...@undefinedvariable
    cursor.execute("SELECT setval('%s'::regclass, %s);" %
(seqname,max_id))
    return max_id

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to