diff --git a/fkreplace/__init__.py b/fkreplace/__init__.py index 5b9f8a4..0b554ef 100644 --- a/fkreplace/__init__.py +++ b/fkreplace/__init__.py @@ -31,10 +31,14 @@ def merge(from_obj, to_obj): for related in from_obj._meta.get_all_related_many_to_many_objects(): accessor_name = related.get_accessor_name() - if accessor_name: - varname = related.field.name - field = getattr(from_obj, accessor_name) - if related.many_to_many: - for f in field.all(): - getattr(f, varname).remove(from_obj) - getattr(f, varname).add(to_obj) + varname = related.field.name + + if not accessor_name: + # not set in M2M to self, but varname will match + accessor_name = varname + + field = getattr(from_obj, accessor_name) + if related.many_to_many: + for f in field.all(): + getattr(f, varname).remove(from_obj) + getattr(f, varname).add(to_obj) diff --git a/tests/tests.py b/tests/tests.py index f0fc2f9..dfc0efd 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -68,4 +68,4 @@ class MergeTests(TestCase): merge(self.a, self.b) assert self.a.friends.all().count() == 0 assert self.b.friends.get().name == f.name - assert f.get().name == self.b.name + assert f.friends.get().name == self.b.name