Misc cleanup

This commit is contained in:
Jeremy Stretch 2025-07-24 10:02:24 -04:00
parent 943f98e86d
commit 944ea00a86
2 changed files with 36 additions and 24 deletions

View File

@ -32,50 +32,63 @@ class ObjectTypeManager(models.Manager):
def get_queryset(self): def get_queryset(self):
return ObjectTypeQuerySet(self.model, using=self._db) return ObjectTypeQuerySet(self.model, using=self._db)
def create(self, **kwargs): def get_by_natural_key(self, app_label, model):
return self.get_queryset().create(**kwargs) """
Retrieve an ObjectType by its application label & model name.
This method exists to provide parity with ContentTypeManager.
"""
return self.get(app_label=app_label, model=model)
def get_for_id(self, id):
"""
Retrieve an ObjectType by its primary key (numeric ID).
This method exists to provide parity with ContentTypeManager.
"""
return self.get(pk=id)
def _get_opts(self, model, for_concrete_model): def _get_opts(self, model, for_concrete_model):
if for_concrete_model: if for_concrete_model:
model = model._meta.concrete_model model = model._meta.concrete_model
return model._meta return model._meta
def get_by_natural_key(self, app_label, model):
return self.get(app_label=app_label, model=model)
def get_for_id(self, id):
return self.get(pk=id)
def get_for_model(self, model, for_concrete_model=True): def get_for_model(self, model, for_concrete_model=True):
"""
Retrieve or create and return the ObjectType for a model.
"""
from netbox.models.features import get_model_features, model_is_public from netbox.models.features import get_model_features, model_is_public
opts = self._get_opts(model, for_concrete_model) opts = self._get_opts(model, for_concrete_model)
try: try:
# Start with get() and not get_or_create() in order to use # Use .get() instead of .get_or_create() initially to ensure db_for_read is honored (Django bug #20401).
# the db_for_read (see #20401).
ot = self.get(app_label=opts.app_label, model=opts.model_name) ot = self.get(app_label=opts.app_label, model=opts.model_name)
except self.model.DoesNotExist: except self.model.DoesNotExist:
# Not found in the database; we proceed to create it. This time # If the ObjectType doesn't exist, create it. (Use .get_or_create() to avoid race conditions.)
# use get_or_create to take care of any race conditions. ot = self.get_or_create(
ot, __ = self.get_or_create(
app_label=opts.app_label, app_label=opts.app_label,
model=opts.model_name, model=opts.model_name,
public=model_is_public(model), public=model_is_public(model),
features=get_model_features(model.__class__), features=get_model_features(model.__class__),
) )[0]
return ot return ot
def public(self): def public(self):
""" """
Filter the base queryset to return only ObjectTypes corresponding to "public" models; those which are intended Includes only ObjectTypes for "public" models.
for reference by other objects.
Filter the base queryset to return only ObjectTypes corresponding to public models; those which are intended
for reference by other objects within the application.
""" """
return self.get_queryset().filter(public=True) return self.get_queryset().filter(public=True)
def with_feature(self, feature): def with_feature(self, feature):
""" """
Return the ContentTypes only for models which are registered as supporting the specified feature. For example, Return ObjectTypes only for models which support the given feature.
we can find all ContentTypes for models which support event rules with:
Only ObjectTypes which list the specified feature will be included. Supported features are declared in
netbox.models.features.FEATURES_MAP. For example, we can find all ObjectTypes for models which support event
rules with:
ObjectType.objects.with_feature('event_rules') ObjectType.objects.with_feature('event_rules')
""" """

View File

@ -45,15 +45,14 @@ clear_events = Signal()
@receiver(post_migrate) @receiver(post_migrate)
def update_object_types(sender, **kwargs): def update_object_types(sender, **kwargs):
models = sender.get_models() """
Create or update the corresponding ObjectType for each model within the migrated app.
for model in models: """
for model in sender.get_models():
app_label, model_name = model._meta.label_lower.split('.') app_label, model_name = model._meta.label_lower.split('.')
# Determine whether model is public # Determine whether model is public and its supported features
is_public = model_is_public(model) is_public = model_is_public(model)
# Determine NetBox features supported by the model
features = get_model_features(model) features = get_model_features(model)
# Create/update the ObjectType for the model # Create/update the ObjectType for the model