Skip to content

Commit 6ead766

Browse files
committed
Custom class for ObjectType
1 parent 7cb067b commit 6ead766

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

netbox_custom_objects/forms.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
from django import forms
44
from django.utils.translation import gettext_lazy as _
55

6-
from netbox_custom_objects.models import CustomObject, CustomObjectType, CustomObjectTypeField, CustomObjectRelation
6+
from netbox_custom_objects.models import (
7+
CustomObject, CustomObjectType, CustomObjectTypeField, CustomObjectRelation, CustomObjectObjectType
8+
)
79

810
from netbox.forms import NetBoxModelForm
911
from core.models import ObjectType
@@ -53,11 +55,12 @@ class CustomObjectTypeFieldForm(CustomFieldForm):
5355
required=True,
5456
label=_('Custom object type')
5557
)
56-
# related_object_type = ContentTypeChoiceField(
57-
# label=_('Related object type'),
58-
# queryset=ObjectType.objects.public(),
59-
# help_text=_("Type of the related object (for object/multi-object fields only)")
60-
# )
58+
related_object_type = ContentTypeChoiceField(
59+
label=_('Related object type'),
60+
queryset=CustomObjectObjectType.objects.public(),
61+
# choices=[("foo", "bar")],
62+
help_text=_("Type of the related object (for object/multi-object fields only)")
63+
)
6164

6265
fieldsets = (
6366
FieldSet(
@@ -84,6 +87,8 @@ def __init__(self, *args, **kwargs):
8487
if self.instance.pk:
8588
self.fields['custom_object_type'].disabled = True
8689

90+
# self.fields['related_object_type'].choices = [("foo", "bar")]
91+
8792

8893
class CustomObjectForm(NetBoxModelForm):
8994
fieldsets = (

netbox_custom_objects/models.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
import json
44
import re
55
from datetime import datetime, date
6+
from copy import deepcopy
67

78
import django_filters
89
from django import forms
910
from django.conf import settings
1011
from django.db import models
11-
from django.db.models import F, Func, Value
12+
from django.db.models import F, Func, Value, Q, QuerySet
1213
from django.db.models.expressions import RawSQL
1314
from django.apps import apps
1415
from django.contrib.contenttypes.models import ContentType
@@ -17,6 +18,7 @@
1718
from django.utils.html import escape
1819
from django.utils.safestring import mark_safe
1920
from django.utils.translation import gettext_lazy as _
21+
from core.models.contenttypes import ObjectTypeManager
2022
from netbox.models import NetBoxModel, ChangeLoggedModel
2123
from netbox.models.features import (
2224
BookmarksMixin,
@@ -31,6 +33,7 @@
3133
TagsMixin,
3234
EventRulesMixin,
3335
)
36+
from netbox.registry import registry
3437
from extras.choices import (
3538
CustomFieldTypeChoices, CustomFieldFilterLogicChoices, CustomFieldUIVisibleChoices, CustomFieldUIEditableChoices
3639
)
@@ -948,3 +951,28 @@ class CustomObjectRelation(models.Model):
948951
def instance(self):
949952
model_class = self.field.related_object_type.model_class()
950953
return model_class.objects.get(pk=self.object_id)
954+
955+
956+
class CustomObjectObjectTypeManager(ObjectTypeManager):
957+
958+
def public(self):
959+
"""
960+
Filter the base queryset to return only ContentTypes corresponding to "public" models; those which are listed
961+
in registry['models'] and intended for reference by other objects.
962+
"""
963+
q = Q()
964+
model_registry = deepcopy(registry['models'])
965+
model_registry['netbox_custom_objects'] = self.get_queryset().values_list('model', flat=True)
966+
for app_label, models in model_registry.items():
967+
q |= Q(app_label=app_label, model__in=models)
968+
return self.get_queryset().filter(q)
969+
970+
971+
class CustomObjectObjectType(ContentType):
972+
"""
973+
Wrap Django's native ContentType model to use our custom manager.
974+
"""
975+
objects = CustomObjectObjectTypeManager()
976+
977+
class Meta:
978+
proxy = True

0 commit comments

Comments
 (0)