Django-transplant attempts to split logic that performs User account merges
into atomic chunks that can be easily and separately maintained.
Surgeon classes perform these tasks.
Surgery class’ constructor accepts two string arguments:
def __init__(self, model, surgeon): ...
It tries to instantiate instances of provided classes dynamically and it
will raise appropriate errors if this is impossible. Django-transplant’s
Surgery class accepts positional argumetn
is a string representing manager that will be provided to
Example use case is:
my_surgery = Surgery( 'myapp.models.Message', 'myapp.models.DefaultSurgeon', manager='sent', )
This will create a surgery that will grab
Message class, get its
manager and provide it to
Surgeon also provides a
merge(receiver, donor) method that just calls
receiver should be the instance of
User that requests the merge,
donor is the User that should be ‘merged
In your views you will probably want to use Surgery classes like this:
# build a list of surgeries surgeries =  surgeries.append(Surgery(...)) ... # perform merge using each surgery object for surgery in surgeries: surgery.merge(self.request.user, some_other_user)
Django-transplant provides three generic
Surgeon classes. They reside in
transplant.surgeons module. Each of them implements a single
method which takes two arguments - receiver and donor User instances.
This method accepts a keyword argument
user_field which should be used
on provided model to change the field that will be updated during the merge.
Surgeonjust sets up
self.user_fieldwith an instance of
stringrespectively. It’s merge method does nothing, but you are encouraged to subclass
NopSurgeonif writing new
- Subclass of
NopSurgeon. Its merge method will:
donor.is_activeto false and donor will be saved.
- get all objects from provided
Managerand set their field provided by ‘user_field’ to
- will call save on all objects from manager, so that all signals are triggered.
- Subclass of
- Works exactly like
DefaultSurgeonbut won’t call save methods. No signals will be triggered.
Writing new subclasses of
Surgery is easy.
While subclassing or writing new
Surgery classes pleas follow the
__init__ takes positional argument
is provided later on to
Surgeon to keep consistennt with
Surgeon classes override
merge following the
convention to accept