handle self-references
This commit is contained in:
parent
8e603c6329
commit
8d4bab0563
@ -31,8 +31,12 @@ def merge(from_obj, to_obj):
|
|||||||
|
|
||||||
for related in from_obj._meta.get_all_related_many_to_many_objects():
|
for related in from_obj._meta.get_all_related_many_to_many_objects():
|
||||||
accessor_name = related.get_accessor_name()
|
accessor_name = related.get_accessor_name()
|
||||||
if accessor_name:
|
|
||||||
varname = related.field.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)
|
field = getattr(from_obj, accessor_name)
|
||||||
if related.many_to_many:
|
if related.many_to_many:
|
||||||
for f in field.all():
|
for f in field.all():
|
||||||
|
@ -68,4 +68,4 @@ class MergeTests(TestCase):
|
|||||||
merge(self.a, self.b)
|
merge(self.a, self.b)
|
||||||
assert self.a.friends.all().count() == 0
|
assert self.a.friends.all().count() == 0
|
||||||
assert self.b.friends.get().name == f.name
|
assert self.b.friends.get().name == f.name
|
||||||
assert f.get().name == self.b.name
|
assert f.friends.get().name == self.b.name
|
||||||
|
Loading…
Reference in New Issue
Block a user