Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion netbox/core/models/object_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.contrib.postgres.fields import ArrayField
from django.contrib.postgres.indexes import GinIndex
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from django.db import connection, models
from django.db.models import Q
from django.utils.translation import gettext as _

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

# 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),
# fall back to ContentType.
if 'core_objecttype' not in connection.introspection.table_names():
ct = ContentType.objects.get_for_model(model, for_concrete_model=for_concrete_model)
ct.features = get_model_features(ct.model_class())
return ct

if not inspect.isclass(model):
model = model.__class__
opts = self._get_opts(model, for_concrete_model)
Expand Down
11 changes: 8 additions & 3 deletions netbox/netbox/models/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,10 +673,15 @@ def has_feature(model_or_ct, feature):
# If an ObjectType was passed, we can use it directly
if type(model_or_ct) is ObjectType:
ot = model_or_ct
# If a ContentType was passed, resolve its model class
# If a ContentType was passed, resolve its model class and run the associated feature test
elif type(model_or_ct) is ContentType:
model_class = model_or_ct.model_class()
ot = ObjectType.objects.get_for_model(model_class) if model_class else None
model = model_or_ct.model_class()
try:
test_func = registry['model_features'][feature]
except KeyError:
# Unknown feature
return False
return test_func(model)
# For anything else, look up the ObjectType
else:
ot = ObjectType.objects.get_for_model(model_or_ct)
Expand Down