diff --git a/netbox_custom_objects/field_types.py b/netbox_custom_objects/field_types.py index 060bc8e..1b07af4 100644 --- a/netbox_custom_objects/field_types.py +++ b/netbox_custom_objects/field_types.py @@ -14,16 +14,25 @@ from django.utils.translation import gettext_lazy as _ from extras.choices import CustomFieldTypeChoices, CustomFieldUIEditableChoices from utilities.api import get_serializer_for_model -from utilities.forms.fields import (CSVChoiceField, CSVMultipleChoiceField, - DynamicChoiceField, - DynamicMultipleChoiceField, JSONField, - LaxURLField) +from utilities.forms.fields import ( + CSVChoiceField, + CSVMultipleChoiceField, + DynamicChoiceField, + DynamicMultipleChoiceField, + JSONField, + LaxURLField, +) from utilities.forms.utils import add_blank_choice -from utilities.forms.widgets import (APISelect, APISelectMultiple, DatePicker, - DateTimePicker) +from utilities.forms.widgets import ( + APISelect, + APISelectMultiple, + DatePicker, + DateTimePicker, +) from utilities.templatetags.builtins.filters import linkify, render_markdown from netbox_custom_objects.constants import APP_LABEL +from netbox_custom_objects.utilities import get_custom_object_type_id_from_content_type class FieldType: @@ -335,8 +344,8 @@ def get_model_field(self, field, **kwargs): if content_type.app_label == APP_LABEL: from netbox_custom_objects.models import CustomObjectType - custom_object_type_id = content_type.model.replace("table", "").replace( - "model", "" + custom_object_type_id = get_custom_object_type_id_from_content_type( + content_type ) custom_object_type = CustomObjectType.objects.get(pk=custom_object_type_id) model = custom_object_type.get_model() @@ -358,12 +367,13 @@ def get_form_field(self, field, for_csv_import=False, **kwargs): content_type = ContentType.objects.get(pk=field.related_object_type_id) from utilities.forms.fields import DynamicModelChoiceField + if content_type.app_label == APP_LABEL: # This is a custom object type from netbox_custom_objects.models import CustomObjectType - custom_object_type_id = content_type.model.replace("table", "").replace( - "model", "" + custom_object_type_id = get_custom_object_type_id_from_content_type( + content_type ) custom_object_type = CustomObjectType.objects.get(pk=custom_object_type_id) model = custom_object_type.get_model() @@ -569,9 +579,7 @@ def get_through_model(self, field, model=None): # Check if this is a self-referential M2M content_type = ContentType.objects.get(pk=field.related_object_type_id) - custom_object_type_id = content_type.model.replace("table", "").replace( - "model", "" - ) + custom_object_type_id = get_custom_object_type_id_from_content_type(content_type) is_self_referential = ( content_type.app_label == APP_LABEL and field.custom_object_type.id == custom_object_type_id @@ -606,9 +614,7 @@ def get_model_field(self, field, **kwargs): """ # Check if this is a self-referential M2M content_type = ContentType.objects.get(pk=field.related_object_type_id) - custom_object_type_id = content_type.model.replace("table", "").replace( - "model", "" - ) + custom_object_type_id = get_custom_object_type_id_from_content_type(content_type) # TODO: Default does not auto-populate, to new or existing objects (should it?) kwargs.update({"default": field.default, "unique": field.unique}) @@ -649,8 +655,8 @@ def get_form_field(self, field, for_csv_import=False, **kwargs): # This is a custom object type from netbox_custom_objects.models import CustomObjectType - custom_object_type_id = content_type.model.replace("table", "").replace( - "model", "" + custom_object_type_id = get_custom_object_type_id_from_content_type( + content_type ) custom_object_type = CustomObjectType.objects.get(pk=custom_object_type_id) model = custom_object_type.get_model() @@ -711,8 +717,8 @@ def after_model_generation(self, instance, model, field_name): if content_type.app_label == APP_LABEL: from netbox_custom_objects.models import CustomObjectType - custom_object_type_id = content_type.model.replace("table", "").replace( - "model", "" + custom_object_type_id = get_custom_object_type_id_from_content_type( + content_type ) custom_object_type = CustomObjectType.objects.get(pk=custom_object_type_id) to_model = custom_object_type.get_model() @@ -753,8 +759,8 @@ def create_m2m_table(self, instance, model, field_name): if content_type.app_label == APP_LABEL: from netbox_custom_objects.models import CustomObjectType - custom_object_type_id = content_type.model.replace("table", "").replace( - "model", "" + custom_object_type_id = get_custom_object_type_id_from_content_type( + content_type ) custom_object_type = CustomObjectType.objects.get( pk=custom_object_type_id diff --git a/netbox_custom_objects/utilities.py b/netbox_custom_objects/utilities.py index f7f6b32..2c85f09 100644 --- a/netbox_custom_objects/utilities.py +++ b/netbox_custom_objects/utilities.py @@ -4,6 +4,7 @@ __all__ = ( "AppsProxy", + "get_custom_object_type_id_from_content_type", "get_viewname", ) @@ -83,3 +84,7 @@ def get_viewname(model, action=None, rest_api=False): viewname = f"{viewname}_{action}" return viewname + + +def get_custom_object_type_id_from_content_type(content_type): + return int(content_type.model.replace("table", "").replace("model", ""))