Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 28 additions & 22 deletions netbox_custom_objects/field_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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})

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions netbox_custom_objects/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

__all__ = (
"AppsProxy",
"get_custom_object_type_id_from_content_type",
"get_viewname",
)

Expand Down Expand Up @@ -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", ""))
Loading