@@ -52,16 +52,49 @@ class CustomObjectsPluginConfig(PluginConfig):
52
52
required_settings = []
53
53
template_extensions = "template_content.template_extensions"
54
54
55
+ def ready (self ):
56
+ from .models import CustomObjectType
57
+ from netbox_custom_objects .api .serializers import get_serializer_class
58
+
59
+ # Suppress warnings about database calls during app initialization
60
+ with warnings .catch_warnings ():
61
+ warnings .filterwarnings (
62
+ "ignore" , category = RuntimeWarning , message = ".*database.*"
63
+ )
64
+ warnings .filterwarnings (
65
+ "ignore" , category = UserWarning , message = ".*database.*"
66
+ )
67
+
68
+ # Skip database calls if running during migration or if table doesn't exist
69
+ if is_running_migration () or not check_custom_object_type_table_exists ():
70
+ super ().ready ()
71
+ return
72
+
73
+ qs = CustomObjectType .objects .all ()
74
+ for obj in qs :
75
+ model = obj .get_model ()
76
+ get_serializer_class (model )
77
+
78
+ super ().ready ()
79
+
55
80
def get_model (self , model_name , require_ready = True ):
56
- try :
57
- # if the model is already loaded, return it
58
- return super ().get_model (model_name , require_ready )
59
- except LookupError :
60
- try :
61
- self .apps .check_apps_ready ()
62
- except AppRegistryNotReady :
63
- raise
81
+ if "table" in model_name .lower () and "model" in model_name .lower ():
82
+ is_custom_object_model = True
83
+ else :
84
+ is_custom_object_model = False
64
85
86
+ self .apps .check_apps_ready ()
87
+ if not is_custom_object_model :
88
+ try :
89
+ # if the model is already loaded, return it
90
+ return super ().get_model (model_name , require_ready )
91
+ except LookupError :
92
+ try :
93
+ self .apps .check_apps_ready ()
94
+ except AppRegistryNotReady :
95
+ raise
96
+
97
+ model_name = model_name .lower ()
65
98
# only do database calls if we are sure the app is ready to avoid
66
99
# Django warnings
67
100
if "table" not in model_name .lower () or "model" not in model_name .lower ():
0 commit comments