fix(core): Cache table existence for ObjectType checks

Introduces a cached `_table_exists` flag to avoid repeated database
introspection queries for `core_objecttype`.
Improves performance during ObjectType lookups and reduces
redundant query overhead.

Fixes #21231
This commit is contained in:
Martin Hauser
2026-01-20 16:17:00 +01:00
committed by Jeremy Stretch
parent 62b9025a9e
commit 39f11f28fb

View File

@@ -35,6 +35,10 @@ class ObjectTypeQuerySet(models.QuerySet):
class ObjectTypeManager(models.Manager): class ObjectTypeManager(models.Manager):
# TODO: Remove this in NetBox v5.0
# Cache the result of introspection to avoid repeated queries.
_table_exists = False
def get_queryset(self): def get_queryset(self):
return ObjectTypeQuerySet(self.model, using=self._db) return ObjectTypeQuerySet(self.model, using=self._db)
@@ -69,10 +73,12 @@ class ObjectTypeManager(models.Manager):
# TODO: Remove this in NetBox v5.0 # TODO: Remove this in NetBox v5.0
# If the ObjectType table has not yet been provisioned (e.g. because we're in a pre-v4.4 migration), # If the ObjectType table has not yet been provisioned (e.g. because we're in a pre-v4.4 migration),
# fall back to ContentType. # fall back to ContentType.
if not ObjectTypeManager._table_exists:
if 'core_objecttype' not in connection.introspection.table_names(): if 'core_objecttype' not in connection.introspection.table_names():
ct = ContentType.objects.get_for_model(model, for_concrete_model=for_concrete_model) ct = ContentType.objects.get_for_model(model, for_concrete_model=for_concrete_model)
ct.features = get_model_features(ct.model_class()) ct.features = get_model_features(ct.model_class())
return ct return ct
ObjectTypeManager._table_exists = True
if not inspect.isclass(model): if not inspect.isclass(model):
model = model.__class__ model = model.__class__