def clone(self, cascade=None, overrides=None, user=None):
        """
        Clone this instance and return the new, cloned instance.

        ``overrides`` should be a dictionary of override values for fields on
        the cloned instance.

        M2M or reverse FK relations listed in ``cascade`` iterable will be
        cascade-cloned. By default, if not listed in ``cascade``, m2m/reverse
        FKs will effectively be cleared (as the remote object will still be
        pointing to the original instance, not the cloned one.)

        If ``cascade`` is a dictionary, keys are m2m/reverse-FK accessor names,
        and values are a callable that takes the queryset of all related
        objects and returns those that should be cloned.

        """
        if cascade is None:
            cascade = {}
        else:
            try:
                cascade.iteritems
            except __HOLE__:
                cascade = dict((i, lambda qs: qs) for i in cascade)

        if overrides is None:
            overrides = {}

        overrides["created_on"] = utcnow()
        overrides["created_by"] = user
        overrides["modified_by"] = user

        clone = self.__class__()

        for field in self._meta.fields:
            if field.primary_key:
                continue
            val = overrides.get(field.name, getattr(self, field.name))
            setattr(clone, field.name, val)

        clone.save(force_insert=True)

        for name, filter_func in cascade.items():
            mgr = getattr(self, name)
            if mgr.__class__.__name__ == "ManyRelatedManager":  # M2M
                clone_mgr = getattr(clone, name)
                existing = set(clone_mgr.all())
                new = set(filter_func(mgr.all()))
                clone_mgr.add(*new.difference(existing))
                clone_mgr.remove(*existing.difference(new))
            elif mgr.__class__.__name__ == "RelatedManager":  # reverse FK
                reverse_name = getattr(self.__class__, name).related.field.name
                for obj in filter_func(mgr.all()):
                    obj.clone(overrides={reverse_name: clone})
            else:
                raise ValueError(
                    "Cannot cascade-clone '{0}'; "
                    "not a many-to-many or reverse foreignkey.".format(name))

        return clone
dataset/ETHPy150Open mozilla/moztrap/moztrap/model/mtmodel.py/MTModel.clone