11import warnings
2+ import sys
23
34from django .core .exceptions import AppRegistryNotReady
4- from django .db import utils as django_db_utils
55from netbox .plugins import PluginConfig
66
77
8+ def is_running_migration ():
9+ """
10+ Check if the code is currently running during a Django migration.
11+ """
12+ # Check if 'makemigrations' or 'migrate' command is in sys.argv
13+ if any (cmd in sys .argv for cmd in ['makemigrations' , 'migrate' ]):
14+ return True
15+
16+ return False
17+
18+
819# Plugin Configuration
920class CustomObjectsPluginConfig (PluginConfig ):
1021 name = "netbox_custom_objects"
@@ -46,11 +57,7 @@ def get_model(self, model_name, require_ready=True):
4657 raise LookupError (
4758 "App '%s' doesn't have a '%s' model." % (self .label , model_name )
4859 )
49- except (django_db_utils .ProgrammingError , django_db_utils .OperationalError ):
50- # Handle database errors that occur when the table doesn't exist yet during migrations
51- raise LookupError (
52- "App '%s' doesn't have a '%s' model." % (self .label , model_name )
53- )
60+
5461 return obj .get_model ()
5562
5663 def get_models (self , include_auto_created = False , include_swapped = False ):
@@ -59,6 +66,10 @@ def get_models(self, include_auto_created=False, include_swapped=False):
5966 for model in super ().get_models (include_auto_created , include_swapped ):
6067 yield model
6168
69+ # Skip custom object type model loading if running during migration
70+ if is_running_migration ():
71+ return
72+
6273 # Suppress warnings about database calls during model loading
6374 with warnings .catch_warnings ():
6475 warnings .filterwarnings (
@@ -73,17 +84,13 @@ def get_models(self, include_auto_created=False, include_swapped=False):
7384
7485 # Only load models that are already cached to avoid creating all models at startup
7586 # This prevents the "two TaggableManagers with same through model" error
76- try :
77- custom_object_types = CustomObjectType .objects .all ()
78- for custom_type in custom_object_types :
79- # Only yield already cached models during discovery
80- if CustomObjectType .is_model_cached (custom_type .id ):
81- model = CustomObjectType .get_cached_model (custom_type .id )
82- if model :
83- yield model
84- except (django_db_utils .ProgrammingError , django_db_utils .OperationalError ):
85- # Handle database errors that occur when the table doesn't exist yet during migrations
86- pass
87+ custom_object_types = CustomObjectType .objects .all ()
88+ for custom_type in custom_object_types :
89+ # Only yield already cached models during discovery
90+ if CustomObjectType .is_model_cached (custom_type .id ):
91+ model = CustomObjectType .get_cached_model (custom_type .id )
92+ if model :
93+ yield model
8794
8895
8996config = CustomObjectsPluginConfig
0 commit comments