Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid unnecessary saves #121

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion django_super_deduper/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ def merge(self, alias_object: Model):
elif related_field.many_to_many:
self._handle_m2m_related_field(related_field, alias_object)

primary_field_has_changed = False
if self.merge_field_values:
# This step can lead to validation errors if `field` has a `unique or` `unique_together` constraint.
for field in model_meta.editable_fields:
Expand All @@ -158,9 +159,11 @@ def merge(self, alias_object: Model):
if primary_value in field.empty_values and alias_value not in field.empty_values:
logger.debug(f'Setting primary {field.name} to alias value: {alias_value}')
setattr(primary_object, field.name, alias_value)
primary_field_has_changed = True

if not self.keep_old:
logger.debug(f'Deleting alias object {self.model_meta.model_name}[pk={alias_object.pk}]')
alias_object.delete()

primary_object.save()
if primary_field_has_changed:
primary_object.save()