Skip to content

Commit

Permalink
Resolve bug cloning OneToOne fields (#493)
Browse files Browse the repository at this point in the history
Fixes: #488
  • Loading branch information
jackton1 authored Oct 3, 2021
1 parent dfde1ce commit 5a2e65d
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions model_clone/mixins/clone.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,13 +464,29 @@ def __duplicate_o2o_fields(self, duplicate, using=None):
):
rel_object = getattr(self, f.name, None)
if rel_object:
new_rel_object = CloneMixin._create_copy_of_instance(
rel_object,
force=True,
sub_clone=True,
)
setattr(new_rel_object, f.remote_field.name, duplicate)
new_rel_object.save(using=using)
if hasattr(rel_object, "make_clone"):
try:
rel_object.make_clone(
attrs={f.remote_field.name: duplicate},
using=using,
)
except IntegrityError:
rel_object.make_clone(
attrs={f.remote_field.name: duplicate},
save_new=False,
sub_clone=True,
using=using,
)
else:
new_rel_object = CloneMixin._create_copy_of_instance(
rel_object,
force=True,
sub_clone=True,
using=using,
)
setattr(new_rel_object, f.remote_field.name, duplicate)

new_rel_object.save(using=using)

return duplicate

Expand Down

0 comments on commit 5a2e65d

Please sign in to comment.