|
1 | 1 | import warnings |
2 | 2 |
|
3 | 3 | from django.core.exceptions import AppRegistryNotReady |
| 4 | +from django.db.utils import DatabaseError, OperationalError |
4 | 5 | from netbox.plugins import PluginConfig |
5 | 6 |
|
6 | 7 |
|
@@ -53,39 +54,27 @@ def get_models(self, include_auto_created=False, include_swapped=False): |
53 | 54 | for model in super().get_models(include_auto_created, include_swapped): |
54 | 55 | yield model |
55 | 56 |
|
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 | + ) |
69 | 65 |
|
70 | | - # Add custom object type models |
71 | | - from .models import CustomObjectType |
| 66 | + # Add custom object type models |
| 67 | + from .models import CustomObjectType |
72 | 68 |
|
| 69 | + custom_object_types = CustomObjectType.objects.all() |
| 70 | + for custom_type in custom_object_types: |
73 | 71 | 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 |
83 | 75 | 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 |
89 | 78 |
|
90 | 79 |
|
91 | 80 | config = CustomObjectsPluginConfig |
0 commit comments