### Navigation

- index
- modules
- toc
- jmb.core 0.8.6 documentation »
- Module code »
- jmb.core.db »

# Source code for jmb.core.db.utils

    """
    DB Utils
    =========

    Il modulo fornisce database utilities per i models

    -------------

    .. autofunction:: clone

    """
    try:
        from django.db.transaction import set_autocommit
        set_autocommit = set_autocommit
    except ImportError:
        from django.db.transaction import commit_manually
        set_autocommit = commit_manually

    @set_autocommit
    def clone(obj):
        """Crea un'istanza di un modello con i valori di un'altra instanza esistente (tranne l'id), comprese
        le relazioni m2m e fk.

        :param obj: instanza modello
        """
        try:
            obj_new = obj.__class__()
            obj_new.__dict__.update(obj.__dict__)
            obj_new.pk = None
            obj_new.save()
            # foreignkey
            for related in obj._meta.get_all_related_objects():
                setattr(obj_new, related.get_accessor_name(), getattr(
                    obj, related.get_accessor_name()).all())
            obj_new.save()
            # many-to-many
            for m2m_field in obj._meta.many_to_many:
                setattr(obj_new, m2m_field.attname, getattr(obj, m2m_field.attname).all())
                #setattr(obj_new, m2m_field.attname, getattr(obj, m2m_field.attname).all())
            obj_new.save()
            transaction.commit()
            return (obj_new, True)
        except Exception, e:
            transaction.rollback()
            return (e, False)

### Navigation

- index
- modules
- toc
- jmb.core 0.8.6 documentation »
- Module code »
- jmb.core.db »