handle self-references

This commit is contained in:
James Turk 2015-05-28 16:06:48 -04:00
parent 8e603c6329
commit 8d4bab0563
2 changed files with 12 additions and 8 deletions

View File

@ -31,8 +31,12 @@ 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
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():

View File

@ -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