Skip to content

Commit e8980d5

Browse files
authored
136 fix branch reversion (#281)
* 136 fix branch reversion * 136 fix branch reversion * 136 fix branch reversion
1 parent bf58d79 commit e8980d5

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

netbox_branching/models/changes.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,19 @@ def undo(self, using=DEFAULT_DB_ALIAS, logger=None):
8181

8282
# Restoring a deleted object
8383
elif self.action == ObjectChangeActionChoices.ACTION_DELETE:
84-
instance = deserialize_object(model, self.prechange_data, pk=self.changed_object_id)
84+
deserialized = deserialize_object(model, self.prechange_data_clean, pk=self.changed_object_id)
85+
instance = deserialized.object
8586
logger.debug(f'Restoring {model._meta.verbose_name} {instance}')
86-
instance.object.full_clean()
87+
88+
# Restore GenericForeignKey fields
89+
for field in instance._meta.private_fields:
90+
if isinstance(field, GenericForeignKey):
91+
ct_field = getattr(instance, field.ct_field)
92+
fk_field = getattr(instance, field.fk_field)
93+
if ct_field and fk_field:
94+
setattr(instance, field.name, ct_field.get_object_for_this_type(pk=fk_field))
95+
96+
instance.full_clean()
8797
instance.save(using=using)
8898

8999
undo.alters_data = True

0 commit comments

Comments
 (0)