Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion netbox_branching/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class AppConfig(PluginConfig):
description = 'A git-like branching implementation for NetBox'
version = '0.7.0'
base_url = 'branching'
min_version = '4.4.0'
min_version = '4.4.1'
max_version = '4.4.99'
middleware = [
'netbox_branching.middleware.BranchMiddleware'
Expand Down
6 changes: 5 additions & 1 deletion netbox_branching/models/changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,18 @@ def apply(self, branch, using=DEFAULT_DB_ALIAS, logger=None):

# Creating a new object
if self.action == ObjectChangeActionChoices.ACTION_CREATE:
instance = deserialize_object(model, self.postchange_data, pk=self.changed_object_id)
if hasattr(model, 'deserialize_object'):
instance = model.deserialize_object(self.postchange_data, pk=self.changed_object_id)
else:
instance = deserialize_object(model, self.postchange_data, pk=self.changed_object_id)
logger.debug(f'Creating {model._meta.verbose_name} {instance}')
instance.object.full_clean()
instance.save(using=using)

# Modifying an object
elif self.action == ObjectChangeActionChoices.ACTION_UPDATE:
instance = model.objects.using(using).get(pk=self.changed_object_id)
logger.debug(f'Updating {model._meta.verbose_name} {instance}')
update_object(instance, self.diff()['post'], using=using)

# Deleting an object
Expand Down
5 changes: 4 additions & 1 deletion netbox_branching/signal_receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ def record_change_diff(instance, **kwargs):
model = instance.changed_object_type.model_class()
with deactivate_branch():
obj = model.objects.get(pk=instance.changed_object_id)
current_data = serialize_object(obj, exclude=['created', 'last_updated'])
if hasattr(obj, 'serialize_object'):
current_data = obj.serialize_object(exclude=['created', 'last_updated'])
else:
current_data = serialize_object(obj, exclude=['created', 'last_updated'])
diff = ChangeDiff(
branch=branch,
object=instance.changed_object,
Expand Down
13 changes: 8 additions & 5 deletions netbox_branching/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from functools import cached_property

from django.contrib import messages
from django.core.exceptions import ObjectDoesNotExist
from django.core.exceptions import FieldDoesNotExist, ObjectDoesNotExist
from django.db.models import ForeignKey, ManyToManyField
from django.http import HttpResponseBadRequest
from django.urls import reverse
Expand Down Expand Up @@ -188,13 +188,16 @@ def update_object(instance, data, using):
if attr == 'custom_fields':
attr = 'custom_field_data'

model_field = instance._meta.get_field(attr)
field_cls = model_field.__class__
try:
model_field = instance._meta.get_field(attr)
field_cls = model_field.__class__
except FieldDoesNotExist:
field_cls = None

if issubclass(field_cls, ForeignKey):
if field_cls and issubclass(field_cls, ForeignKey):
# Direct value assignment for ForeignKeys must be done by the field's concrete name
setattr(instance, f'{attr}_id', value)
elif issubclass(field_cls, (ManyToManyField, TaggableManager)):
elif field_cls and issubclass(field_cls, (ManyToManyField, TaggableManager)):
# Use M2M manager for ManyToMany assignments
m2m_manager = getattr(instance, attr)
m2m_assignments[m2m_manager] = value
Expand Down