From 944ea00a864705dc936f1a1c1eac8fd76215dae6 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 24 Jul 2025 10:02:24 -0400 Subject: [PATCH] Misc cleanup --- netbox/core/models/contenttypes.py | 49 +++++++++++++++++++----------- netbox/core/signals.py | 11 +++---- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/netbox/core/models/contenttypes.py b/netbox/core/models/contenttypes.py index b3d2c4681..964a3a60a 100644 --- a/netbox/core/models/contenttypes.py +++ b/netbox/core/models/contenttypes.py @@ -32,50 +32,63 @@ class ObjectTypeManager(models.Manager): def get_queryset(self): return ObjectTypeQuerySet(self.model, using=self._db) - def create(self, **kwargs): - return self.get_queryset().create(**kwargs) + def get_by_natural_key(self, app_label, model): + """ + 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): if for_concrete_model: model = model._meta.concrete_model 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): + """ + Retrieve or create and return the ObjectType for a model. + """ from netbox.models.features import get_model_features, model_is_public opts = self._get_opts(model, for_concrete_model) try: - # Start with get() and not get_or_create() in order to use - # the db_for_read (see #20401). + # Use .get() instead of .get_or_create() initially to ensure db_for_read is honored (Django bug #20401). ot = self.get(app_label=opts.app_label, model=opts.model_name) except self.model.DoesNotExist: - # Not found in the database; we proceed to create it. This time - # use get_or_create to take care of any race conditions. - ot, __ = self.get_or_create( + # If the ObjectType doesn't exist, create it. (Use .get_or_create() to avoid race conditions.) + ot = self.get_or_create( app_label=opts.app_label, model=opts.model_name, public=model_is_public(model), features=get_model_features(model.__class__), - ) + )[0] return ot def public(self): """ - Filter the base queryset to return only ObjectTypes corresponding to "public" models; those which are intended - for reference by other objects. + Includes only ObjectTypes for "public" models. + + 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) def with_feature(self, feature): """ - Return the ContentTypes only for models which are registered as supporting the specified feature. For example, - we can find all ContentTypes for models which support event rules with: + Return ObjectTypes only for models which support the given feature. + + 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') """ diff --git a/netbox/core/signals.py b/netbox/core/signals.py index 62dc06dcf..d1da76fbf 100644 --- a/netbox/core/signals.py +++ b/netbox/core/signals.py @@ -45,15 +45,14 @@ clear_events = Signal() @receiver(post_migrate) def update_object_types(sender, **kwargs): - models = sender.get_models() - - for model in models: + """ + Create or update the corresponding ObjectType for each model within the migrated app. + """ + for model in sender.get_models(): 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) - - # Determine NetBox features supported by the model features = get_model_features(model) # Create/update the ObjectType for the model