Skip to content

Commit 6930677

Browse files
committed
cleanup
1 parent d071480 commit 6930677

File tree

1 file changed

+18
-29
lines changed

1 file changed

+18
-29
lines changed

netbox_custom_objects/__init__.py

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import warnings
22

33
from django.core.exceptions import AppRegistryNotReady
4+
from django.db.utils import DatabaseError, OperationalError
45
from netbox.plugins import PluginConfig
56

67

@@ -53,39 +54,27 @@ def get_models(self, include_auto_created=False, include_swapped=False):
5354
for model in super().get_models(include_auto_created, include_swapped):
5455
yield model
5556

56-
# Only add dynamic models if we have access to the database
57-
try:
58-
from django.db import connection
59-
connection.ensure_connection()
60-
61-
# Suppress warnings about database calls during model loading
62-
with warnings.catch_warnings():
63-
warnings.filterwarnings(
64-
"ignore", category=RuntimeWarning, message=".*database.*"
65-
)
66-
warnings.filterwarnings(
67-
"ignore", category=UserWarning, message=".*database.*"
68-
)
57+
# Suppress warnings about database calls during model loading
58+
with warnings.catch_warnings():
59+
warnings.filterwarnings(
60+
"ignore", category=RuntimeWarning, message=".*database.*"
61+
)
62+
warnings.filterwarnings(
63+
"ignore", category=UserWarning, message=".*database.*"
64+
)
6965

70-
# Add custom object type models
71-
from .models import CustomObjectType
66+
# Add custom object type models
67+
from .models import CustomObjectType
7268

69+
custom_object_types = CustomObjectType.objects.all()
70+
for custom_type in custom_object_types:
7371
try:
74-
custom_object_types = CustomObjectType.objects.all()
75-
for custom_type in custom_object_types:
76-
try:
77-
model = custom_type.get_model()
78-
if model:
79-
yield model
80-
except Exception:
81-
# Skip models that can't be loaded
82-
continue
72+
model = custom_type.get_model()
73+
if model:
74+
yield model
8375
except Exception:
84-
# Skip if we can't access CustomObjectType (e.g., during migrations)
85-
pass
86-
except Exception:
87-
# Skip dynamic models if database is not available
88-
pass
76+
# Skip models that can't be loaded
77+
continue
8978

9079

9180
config = CustomObjectsPluginConfig

0 commit comments

Comments
 (0)