Skip to content

Commit 26d9a62

Browse files
committed
Cleanup table name resolution
1 parent 2b9f2b7 commit 26d9a62

File tree

3 files changed

+15
-25
lines changed

3 files changed

+15
-25
lines changed

netbox_custom_objects/__init__.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,9 @@ def get_model(self, model_name, require_ready=True):
9999
)
100100

101101
from .models import CustomObjectType
102+
from .utilities import get_custom_object_type_from_content_type
102103

103-
custom_object_type_id = int(
104-
model_name.replace("table", "").replace("model", "")
105-
)
104+
custom_object_type_id = get_custom_object_type_from_content_type(model_name)
106105

107106
try:
108107
obj = CustomObjectType.objects.get(pk=custom_object_type_id)

netbox_custom_objects/field_types.py

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
from utilities.templatetags.builtins.filters import linkify, render_markdown
2525

2626
from netbox_custom_objects.constants import APP_LABEL
27-
27+
from netbox_custom_objects.utilities import get_custom_object_type_from_content_type
2828

2929
class FieldType:
3030

@@ -335,9 +335,7 @@ def get_model_field(self, field, **kwargs):
335335
if content_type.app_label == APP_LABEL:
336336
from netbox_custom_objects.models import CustomObjectType
337337

338-
custom_object_type_id = content_type.model.replace("table", "").replace(
339-
"model", ""
340-
)
338+
custom_object_type_id = get_custom_object_type_from_content_type(content_type)
341339
custom_object_type = CustomObjectType.objects.get(pk=custom_object_type_id)
342340
model = custom_object_type.get_model()
343341
else:
@@ -362,9 +360,7 @@ def get_form_field(self, field, for_csv_import=False, **kwargs):
362360
# This is a custom object type
363361
from netbox_custom_objects.models import CustomObjectType
364362

365-
custom_object_type_id = content_type.model.replace("table", "").replace(
366-
"model", ""
367-
)
363+
custom_object_type_id = get_custom_object_type_from_content_type(content_type)
368364
custom_object_type = CustomObjectType.objects.get(pk=custom_object_type_id)
369365
model = custom_object_type.get_model()
370366
field_class = DynamicModelChoiceField
@@ -569,9 +565,7 @@ def get_through_model(self, field, model=None):
569565

570566
# Check if this is a self-referential M2M
571567
content_type = ContentType.objects.get(pk=field.related_object_type_id)
572-
custom_object_type_id = content_type.model.replace("table", "").replace(
573-
"model", ""
574-
)
568+
custom_object_type_id = get_custom_object_type_from_content_type(content_type)
575569
is_self_referential = (
576570
content_type.app_label == APP_LABEL
577571
and field.custom_object_type.id == custom_object_type_id
@@ -606,9 +600,7 @@ def get_model_field(self, field, **kwargs):
606600
"""
607601
# Check if this is a self-referential M2M
608602
content_type = ContentType.objects.get(pk=field.related_object_type_id)
609-
custom_object_type_id = content_type.model.replace("table", "").replace(
610-
"model", ""
611-
)
603+
custom_object_type_id = get_custom_object_type_from_content_type(content_type)
612604
# TODO: Default does not auto-populate, to new or existing objects (should it?)
613605
kwargs.update({"default": field.default, "unique": field.unique})
614606

@@ -649,9 +641,7 @@ def get_form_field(self, field, for_csv_import=False, **kwargs):
649641
# This is a custom object type
650642
from netbox_custom_objects.models import CustomObjectType
651643

652-
custom_object_type_id = content_type.model.replace("table", "").replace(
653-
"model", ""
654-
)
644+
custom_object_type_id = get_custom_object_type_from_content_type(content_type)
655645
custom_object_type = CustomObjectType.objects.get(pk=custom_object_type_id)
656646
model = custom_object_type.get_model()
657647
else:
@@ -711,9 +701,7 @@ def after_model_generation(self, instance, model, field_name):
711701
if content_type.app_label == APP_LABEL:
712702
from netbox_custom_objects.models import CustomObjectType
713703

714-
custom_object_type_id = content_type.model.replace("table", "").replace(
715-
"model", ""
716-
)
704+
custom_object_type_id = get_custom_object_type_from_content_type(content_type)
717705
custom_object_type = CustomObjectType.objects.get(pk=custom_object_type_id)
718706
to_model = custom_object_type.get_model()
719707
else:
@@ -753,9 +741,7 @@ def create_m2m_table(self, instance, model, field_name):
753741
if content_type.app_label == APP_LABEL:
754742
from netbox_custom_objects.models import CustomObjectType
755743

756-
custom_object_type_id = content_type.model.replace("table", "").replace(
757-
"model", ""
758-
)
744+
custom_object_type_id = get_custom_object_type_from_content_type(content_type)
759745
custom_object_type = CustomObjectType.objects.get(
760746
pk=custom_object_type_id
761747
)

netbox_custom_objects/utilities.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
__all__ = (
66
"AppsProxy",
7+
"get_custom_object_type_from_content_type",
78
"get_viewname",
89
)
910

@@ -83,3 +84,7 @@ def get_viewname(model, action=None, rest_api=False):
8384
viewname = f"{viewname}_{action}"
8485

8586
return viewname
87+
88+
89+
def get_custom_object_type_from_content_type(content_type):
90+
return content_type.model.replace("table", "").replace("model", "")

0 commit comments

Comments
 (0)