Skip to content

Commit 5ceb6a6

Browse files
committed
Fixes #20290: Avoid exceptions when upgrading to v4.4 from early releases due to missing ObjectTypes table
1 parent 33d4759 commit 5ceb6a6

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

netbox/core/models/object_types.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from django.contrib.postgres.fields import ArrayField
66
from django.contrib.postgres.indexes import GinIndex
77
from django.core.exceptions import ObjectDoesNotExist
8-
from django.db import models
8+
from django.db import connection, models
99
from django.db.models import Q
1010
from django.utils.translation import gettext as _
1111

@@ -66,6 +66,14 @@ def get_for_model(self, model, for_concrete_model=True):
6666
"""
6767
from netbox.models.features import get_model_features, model_is_public
6868

69+
# TODO: Remove this in NetBox v5.0
70+
# If the ObjectType table has not yet been provisioned (e.g. because we're in a pre-v4.4 migration),
71+
# fall back to ContentType.
72+
if 'core_objecttype' not in connection.introspection.table_names():
73+
ct = ContentType.objects.get_for_model(model, for_concrete_model=for_concrete_model)
74+
ct.features = get_model_features(ct.model_class())
75+
return ct
76+
6977
if not inspect.isclass(model):
7078
model = model.__class__
7179
opts = self._get_opts(model, for_concrete_model)

netbox/netbox/models/features.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -673,10 +673,15 @@ def has_feature(model_or_ct, feature):
673673
# If an ObjectType was passed, we can use it directly
674674
if type(model_or_ct) is ObjectType:
675675
ot = model_or_ct
676-
# If a ContentType was passed, resolve its model class
676+
# If a ContentType was passed, resolve its model class and run the associated feature test
677677
elif type(model_or_ct) is ContentType:
678-
model_class = model_or_ct.model_class()
679-
ot = ObjectType.objects.get_for_model(model_class) if model_class else None
678+
model = model_or_ct.model_class()
679+
try:
680+
test_func = registry['model_features'][feature]
681+
except KeyError:
682+
# Unknown feature
683+
return False
684+
return test_func(model)
680685
# For anything else, look up the ObjectType
681686
else:
682687
ot = ObjectType.objects.get_for_model(model_or_ct)

0 commit comments

Comments
 (0)