handle self-references
This commit is contained in:
parent
8e603c6329
commit
8d4bab0563
@ -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)
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user