From 158279698a5bd4517123f2c689c7f797d4843660 Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 15 Aug 2025 10:13:50 -0700 Subject: [PATCH 1/4] Cleanup warning messages --- netbox_custom_objects/api/serializers.py | 3 ++- netbox_custom_objects/models.py | 24 +++++++++++++++--------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/netbox_custom_objects/api/serializers.py b/netbox_custom_objects/api/serializers.py index 56d10d3..94672f7 100644 --- a/netbox_custom_objects/api/serializers.py +++ b/netbox_custom_objects/api/serializers.py @@ -1,3 +1,4 @@ +import logging import sys from core.models import ObjectType @@ -251,7 +252,7 @@ def get_display(self, obj): try: attrs[field.name] = field_type.get_serializer_field(field) except NotImplementedError: - print( + logging.debug( f"serializer: {field.name} field is not implemented; using a default serializer field" ) diff --git a/netbox_custom_objects/models.py b/netbox_custom_objects/models.py index 9788911..e79dd10 100644 --- a/netbox_custom_objects/models.py +++ b/netbox_custom_objects/models.py @@ -1,5 +1,6 @@ import decimal import re +import warnings from datetime import date, datetime import django_filters @@ -436,15 +437,20 @@ def wrapped_post_through_setup(self, cls): TM.post_through_setup = wrapped_post_through_setup - try: - model = type( - str(model_name), - (CustomObject, models.Model), - attrs, - ) - finally: - # Restore the original method - TM.post_through_setup = original_post_through_setup + # Suppress RuntimeWarning about model already being registered + # TODO: Remove this once we have a better way to handle model registration + with warnings.catch_warnings(): + warnings.filterwarnings("ignore", category=RuntimeWarning, message=".*was already registered.*") + + try: + model = type( + str(model_name), + (CustomObject, models.Model), + attrs, + ) + finally: + # Restore the original method + TM.post_through_setup = original_post_through_setup # Register the main model with Django's app registry try: From 0992a51bf5f525a7112930ac35fe97738ac17864 Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 15 Aug 2025 10:16:01 -0700 Subject: [PATCH 2/4] Cleanup warning messages --- netbox_custom_objects/models.py | 42 ++++++++++++++------------------- 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/netbox_custom_objects/models.py b/netbox_custom_objects/models.py index e79dd10..206b852 100644 --- a/netbox_custom_objects/models.py +++ b/netbox_custom_objects/models.py @@ -8,7 +8,6 @@ from core.models.contenttypes import ObjectTypeManager from django.apps import apps from django.conf import settings - # from django.contrib.contenttypes.management import create_contenttypes from django.contrib.contenttypes.models import ContentType from django.core.validators import RegexValidator, ValidationError @@ -17,27 +16,18 @@ from django.db.models.functions import Lower from django.urls import reverse from django.utils.translation import gettext_lazy as _ -from extras.choices import ( - CustomFieldFilterLogicChoices, - CustomFieldTypeChoices, - CustomFieldUIEditableChoices, - CustomFieldUIVisibleChoices, -) +from extras.choices import (CustomFieldFilterLogicChoices, + CustomFieldTypeChoices, + CustomFieldUIEditableChoices, + CustomFieldUIVisibleChoices) from extras.models.customfields import SEARCH_TYPES from netbox.models import ChangeLoggedModel, PrimaryModel -from netbox.models.features import ( - BookmarksMixin, - ChangeLoggingMixin, - CloningMixin, - CustomLinksMixin, - CustomValidationMixin, - EventRulesMixin, - ExportTemplatesMixin, - JournalingMixin, - NotificationsMixin, - get_model_features, - TagsMixin, -) +from netbox.models.features import (BookmarksMixin, ChangeLoggingMixin, + CloningMixin, CustomLinksMixin, + CustomValidationMixin, EventRulesMixin, + ExportTemplatesMixin, JournalingMixin, + NotificationsMixin, TagsMixin, + get_model_features) from netbox.registry import registry from utilities import filters from utilities.datetime import datetime_from_timestamp @@ -381,7 +371,8 @@ def get_model( if self.is_model_cached(self.id): model = self.get_cached_model(self.id) # Ensure the serializer is registered even for cached models - from netbox_custom_objects.api.serializers import get_serializer_class + from netbox_custom_objects.api.serializers import \ + get_serializer_class get_serializer_class(model) return model @@ -440,8 +431,10 @@ def wrapped_post_through_setup(self, cls): # Suppress RuntimeWarning about model already being registered # TODO: Remove this once we have a better way to handle model registration with warnings.catch_warnings(): - warnings.filterwarnings("ignore", category=RuntimeWarning, message=".*was already registered.*") - + warnings.filterwarnings( + "ignore", category=RuntimeWarning, message=".*was already registered.*" + ) + try: model = type( str(model_name), @@ -470,7 +463,8 @@ def wrapped_post_through_setup(self, cls): # Register the serializer for this model if not manytomany_models: - from netbox_custom_objects.api.serializers import get_serializer_class + from netbox_custom_objects.api.serializers import \ + get_serializer_class get_serializer_class(model) From f675d3090297368b39f31c5748c4b07b137aff03 Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 15 Aug 2025 10:19:10 -0700 Subject: [PATCH 3/4] Cleanup warning messages --- netbox_custom_objects/models.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/netbox_custom_objects/models.py b/netbox_custom_objects/models.py index ef32713..4f8586f 100644 --- a/netbox_custom_objects/models.py +++ b/netbox_custom_objects/models.py @@ -372,8 +372,7 @@ def get_model( if self.is_model_cached(self.id): model = self.get_cached_model(self.id) # Ensure the serializer is registered even for cached models - from netbox_custom_objects.api.serializers import \ - get_serializer_class + from netbox_custom_objects.api.serializers import get_serializer_class get_serializer_class(model) return model @@ -464,8 +463,7 @@ def wrapped_post_through_setup(self, cls): # Register the serializer for this model if not manytomany_models: - from netbox_custom_objects.api.serializers import \ - get_serializer_class + from netbox_custom_objects.api.serializers import get_serializer_class get_serializer_class(model) From 29f8b10856d19066855e92b5a0d478b8c5cd57c8 Mon Sep 17 00:00:00 2001 From: Arthur Date: Fri, 15 Aug 2025 10:19:50 -0700 Subject: [PATCH 4/4] Cleanup warning messages --- netbox_custom_objects/models.py | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/netbox_custom_objects/models.py b/netbox_custom_objects/models.py index 4f8586f..c4fdba0 100644 --- a/netbox_custom_objects/models.py +++ b/netbox_custom_objects/models.py @@ -8,6 +8,7 @@ from core.models.contenttypes import ObjectTypeManager from django.apps import apps from django.conf import settings + # from django.contrib.contenttypes.management import create_contenttypes from django.contrib.contenttypes.models import ContentType from django.core.validators import RegexValidator, ValidationError @@ -16,18 +17,27 @@ from django.db.models.functions import Lower from django.urls import reverse from django.utils.translation import gettext_lazy as _ -from extras.choices import (CustomFieldFilterLogicChoices, - CustomFieldTypeChoices, - CustomFieldUIEditableChoices, - CustomFieldUIVisibleChoices) +from extras.choices import ( + CustomFieldFilterLogicChoices, + CustomFieldTypeChoices, + CustomFieldUIEditableChoices, + CustomFieldUIVisibleChoices, +) from extras.models.customfields import SEARCH_TYPES from netbox.models import ChangeLoggedModel, PrimaryModel -from netbox.models.features import (BookmarksMixin, ChangeLoggingMixin, - CloningMixin, CustomLinksMixin, - CustomValidationMixin, EventRulesMixin, - ExportTemplatesMixin, JournalingMixin, - NotificationsMixin, TagsMixin, - get_model_features) +from netbox.models.features import ( + BookmarksMixin, + ChangeLoggingMixin, + CloningMixin, + CustomLinksMixin, + CustomValidationMixin, + EventRulesMixin, + ExportTemplatesMixin, + JournalingMixin, + NotificationsMixin, + TagsMixin, + get_model_features, +) from netbox.registry import registry from utilities import filters from utilities.datetime import datetime_from_timestamp