@@ -125,7 +125,7 @@ def get_list_url(self):
125125class CustomObjectType (NetBoxModel ):
126126 # Class-level cache for generated models
127127 _model_cache = {}
128- _through_model_cache = {} # Now stores {custom_object_type_id: {through_model_name: through_model}}
128+ _through_model_cache = {} # Now stores {custom_object_type_id: {through_model_name: through_model}}
129129 name = models .CharField (max_length = 100 , unique = True )
130130 description = models .TextField (blank = True )
131131 schema = models .JSONField (blank = True , default = dict )
@@ -437,7 +437,7 @@ def get_model(
437437
438438 # Cache the generated model and its through models
439439 self ._model_cache [self .id ] = model
440- if self .id not in self ._through_model_cache :
440+ if self .id not in self ._through_model_cache :
441441 self ._through_model_cache [self .id ] = {}
442442 self ._through_model_cache [self .id ][through_model_name ] = through_model
443443 return model
@@ -1136,19 +1136,19 @@ def save(self, *args, **kwargs):
11361136 else :
11371137 old_field = field_type .get_model_field (self .original )
11381138 old_field .contribute_to_class (model , self ._original_name )
1139-
1139+
11401140 # Special handling for MultiObject fields when the name changes
1141- if (self .type == CustomFieldTypeChoices .TYPE_MULTIOBJECT and
1141+ if (self .type == CustomFieldTypeChoices .TYPE_MULTIOBJECT and
11421142 self .name != self ._original_name ):
11431143 # For renamed MultiObject fields, we just need to rename the through table
11441144 old_through_table_name = self .original .through_table_name
11451145 new_through_table_name = self .through_table_name
1146-
1146+
11471147 # Check if old through table exists
11481148 with connection .cursor () as cursor :
11491149 tables = connection .introspection .table_names (cursor )
11501150 old_table_exists = old_through_table_name in tables
1151-
1151+
11521152 if old_table_exists :
11531153 # Create temporary models to represent the old and new through table states
11541154 old_through_meta = type (
@@ -1168,16 +1168,16 @@ def save(self, *args, **kwargs):
11681168 "Meta" : old_through_meta ,
11691169 "id" : models .AutoField (primary_key = True ),
11701170 "source" : models .ForeignKey (
1171- model , on_delete = models .CASCADE ,
1171+ model , on_delete = models .CASCADE ,
11721172 db_column = "source_id" , related_name = "+"
11731173 ),
11741174 "target" : models .ForeignKey (
1175- model , on_delete = models .CASCADE ,
1175+ model , on_delete = models .CASCADE ,
11761176 db_column = "target_id" , related_name = "+"
11771177 ),
11781178 },
11791179 )
1180-
1180+
11811181 new_through_meta = type (
11821182 "Meta" ,
11831183 (),
@@ -1195,22 +1195,23 @@ def save(self, *args, **kwargs):
11951195 "Meta" : new_through_meta ,
11961196 "id" : models .AutoField (primary_key = True ),
11971197 "source" : models .ForeignKey (
1198- model , on_delete = models .CASCADE ,
1198+ model , on_delete = models .CASCADE ,
11991199 db_column = "source_id" , related_name = "+"
12001200 ),
12011201 "target" : models .ForeignKey (
1202- model , on_delete = models .CASCADE ,
1202+ model , on_delete = models .CASCADE ,
12031203 db_column = "target_id" , related_name = "+"
12041204 ),
12051205 },
12061206 )
1207-
1207+ new_through_model # To silence ruff error
1208+
12081209 # Rename the table using Django's schema editor
12091210 schema_editor .alter_db_table (old_through_model , old_through_table_name , new_through_table_name )
12101211 else :
12111212 # No old table exists, create the new through table
12121213 field_type .create_m2m_table (self , model , self .name )
1213-
1214+
12141215 # Alter the field normally (this updates the field definition)
12151216 schema_editor .alter_field (model , old_field , model_field )
12161217 else :
0 commit comments