[Django]-Merge two model instance in Django

3👍

Have you tried this way, first find out those values not null in person2:

person2_dict = {k: v for k, v in person2.__dict__.iteritems() if v is not None}

Merge them into the target person1:

person1.__dict__.update(person2_dict)
person1.save()
👤iMom0

4👍

Two third party libraries can help deduplicate Django models:

Django Extensions’ merge_model_instances management command.

Django Super Deduper

Leave a comment