From 2c07762b7a4f992eac17007d38f45a0dd04b4c1c Mon Sep 17 00:00:00 2001 From: Abhimanyu Saharan Date: Mon, 10 Apr 2023 22:10:13 +0530 Subject: [PATCH] Added optional user and group on custom field (#12206) * added group and user model to object_type * Update netbox/utilities/utils.py Co-authored-by: Jeremy Stretch --------- Co-authored-by: Jeremy Stretch --- netbox/extras/forms/model_forms.py | 3 ++- netbox/utilities/utils.py | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/netbox/extras/forms/model_forms.py b/netbox/extras/forms/model_forms.py index 365d55c72..b7e606f7d 100644 --- a/netbox/extras/forms/model_forms.py +++ b/netbox/extras/forms/model_forms.py @@ -1,6 +1,7 @@ import json from django import forms +from django.db.models import Q from django.contrib.contenttypes.models import ContentType from django.utils.translation import gettext as _ @@ -37,7 +38,7 @@ class CustomFieldForm(BootstrapMixin, forms.ModelForm): object_type = ContentTypeChoiceField( queryset=ContentType.objects.all(), # TODO: Come up with a canonical way to register suitable models - limit_choices_to=FeatureQuery('webhooks'), + limit_choices_to=FeatureQuery('webhooks').get_query() | Q(app_label='auth', model__in=['user', 'group']), required=False, help_text=_("Type of the related object (for object/multi-object fields only)") ) diff --git a/netbox/utilities/utils.py b/netbox/utilities/utils.py index aec0d896c..57092bb7d 100644 --- a/netbox/utilities/utils.py +++ b/netbox/utilities/utils.py @@ -48,6 +48,10 @@ def get_viewname(model, action=None, rest_api=False): if is_plugin: viewname = f'plugins-api:{app_label}-api:{model_name}' else: + # Alter the app_label for group and user model_name to point to users app + if app_label == 'auth' and model_name in ['group', 'user']: + app_label = 'users' + viewname = f'{app_label}-api:{model_name}' # Append the action, if any if action: