@@ -75,9 +75,9 @@ def fk_associations(cls):
7575 """Get the mapping between foreign key (FK) fields and the corresponding DiffSync models they reference."""
7676 return cls ._fk_associations
7777
78- @staticmethod
78+ @classmethod
7979 def _get_nautobot_record (
80- diffsync_model : DiffSyncModel , diffsync_value : Any , fail_quiet : bool = False
80+ cls , diffsync_model : DiffSyncModel , diffsync_value : Any , fail_quiet : bool = False
8181 ) -> Optional [models .Model ]:
8282 """Given a diffsync model and identifier (natural key or primary key) look up the Nautobot record."""
8383 try :
@@ -93,6 +93,7 @@ def _get_nautobot_record(
9393 log = logger .debug if fail_quiet else logger .error
9494 log (
9595 "Expected but did not find an existing Nautobot record" ,
96+ source = cls .get_type (),
9697 target = diffsync_model .get_type (),
9798 unique_id = diffsync_value ,
9899 )
@@ -186,6 +187,7 @@ def clean_attrs(cls, diffsync: DiffSync, attrs: dict) -> Tuple[dict, dict]:
186187 @staticmethod
187188 def create_nautobot_record (nautobot_model , ids : Mapping , attrs : Mapping , multivalue_attrs : Mapping ):
188189 """Helper method to create() - actually populate Nautobot data."""
190+ model_data = dict (** ids , ** attrs , ** multivalue_attrs )
189191 try :
190192 # Custom fields are a special case - because in NetBox the values defined on a particular record are
191193 # only loosely coupled to the CustomField definition itself, it's quite possible that these two may be
@@ -208,23 +210,23 @@ def create_nautobot_record(nautobot_model, ids: Mapping, attrs: Mapping, multiva
208210 action = "create" ,
209211 exception = str (exc ),
210212 model = nautobot_model ,
211- model_data = dict ( ** ids , ** attrs , ** multivalue_attrs ) ,
213+ model_data = model_data ,
212214 )
213215 except DjangoValidationError as exc :
214216 logger .error (
215217 "Nautobot reported a data validation error - check your source data" ,
216218 action = "create" ,
217219 exception = str (exc ),
218220 model = nautobot_model ,
219- model_data = dict ( ** ids , ** attrs , ** multivalue_attrs ) ,
221+ model_data = model_data ,
220222 )
221223 except ObjectDoesNotExist as exc : # Including RelatedObjectDoesNotExist
222224 logger .error (
223225 "Nautobot reported an error about a missing required object" ,
224226 action = "create" ,
225227 exception = str (exc ),
226228 model = nautobot_model ,
227- model_data = dict ( ** ids , ** attrs , ** multivalue_attrs ) ,
229+ model_data = model_data ,
228230 )
229231
230232 return None
@@ -272,9 +274,17 @@ def create(cls, diffsync: DiffSync, ids: Mapping, attrs: Mapping) -> Optional["N
272274 @staticmethod
273275 def update_nautobot_record (nautobot_model , ids : Mapping , attrs : Mapping , multivalue_attrs : Mapping ):
274276 """Helper method to update() - actually update Nautobot data."""
277+ model_data = dict (** ids , ** attrs , ** multivalue_attrs )
275278 try :
276279 record = nautobot_model .objects .get (** ids )
277280 custom_field_data = attrs .pop ("custom_field_data" , None )
281+ # Temporarily clear any existing custom field data as part of the model update,
282+ # so that in case the model contains "stale" data referring to no-longer-existent fields,
283+ # Nautobot won't reject it out of hand.
284+ if not custom_field_data and hasattr (record , "custom_field_data" ):
285+ custom_field_data = record .custom_field_data
286+ if custom_field_data :
287+ record ._custom_field_data = {} # pylint: disable=protected-access
278288 for attr , value in attrs .items ():
279289 setattr (record , attr , value )
280290 record .clean ()
@@ -291,23 +301,23 @@ def update_nautobot_record(nautobot_model, ids: Mapping, attrs: Mapping, multiva
291301 action = "update" ,
292302 exception = str (exc ),
293303 model = nautobot_model ,
294- model_data = dict ( ** ids , ** attrs , ** multivalue_attrs ) ,
304+ model_data = model_data ,
295305 )
296306 except DjangoValidationError as exc :
297307 logger .error (
298308 "Nautobot reported a data validation error - check your source data" ,
299309 action = "update" ,
300310 exception = str (exc ),
301311 model = nautobot_model ,
302- model_data = dict ( ** ids , ** attrs , ** multivalue_attrs ) ,
312+ model_data = model_data ,
303313 )
304314 except ObjectDoesNotExist as exc : # Including RelatedObjectDoesNotExist
305315 logger .error (
306316 "Nautobot reported an error about a missing required object" ,
307317 action = "update" ,
308318 exception = str (exc ),
309319 model = nautobot_model ,
310- model_data = dict ( ** ids , ** attrs , ** multivalue_attrs ) ,
320+ model_data = model_data ,
311321 )
312322
313323 return None
0 commit comments