mirror of
https://github.com/netbox-community/netbox.git
synced 2025-12-20 20:32:25 -06:00
Add UI views for custom fields
This commit is contained in:
@@ -6,8 +6,9 @@ from io import StringIO
|
||||
import django_filters
|
||||
from django import forms
|
||||
from django.conf import settings
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
|
||||
from django.db.models import Count
|
||||
from django.db.models import Count, Q
|
||||
from django.forms import BoundField
|
||||
from django.forms.fields import JSONField as _JSONField, InvalidJSONInput
|
||||
from django.urls import reverse
|
||||
@@ -28,6 +29,7 @@ __all__ = (
|
||||
'CSVContentTypeField',
|
||||
'CSVDataField',
|
||||
'CSVModelChoiceField',
|
||||
'CSVMultipleContentTypeField',
|
||||
'CSVTypedChoiceField',
|
||||
'DynamicModelChoiceField',
|
||||
'DynamicModelMultipleChoiceField',
|
||||
@@ -281,6 +283,20 @@ class CSVContentTypeField(CSVModelChoiceField):
|
||||
raise forms.ValidationError(f'Invalid object type')
|
||||
|
||||
|
||||
class CSVMultipleContentTypeField(forms.ModelMultipleChoiceField):
|
||||
STATIC_CHOICES = True
|
||||
|
||||
# TODO: Improve validation of selected ContentTypes
|
||||
def prepare_value(self, value):
|
||||
if type(value) is str:
|
||||
ct_filter = Q()
|
||||
for name in value.split(','):
|
||||
app_label, model = name.split('.')
|
||||
ct_filter |= Q(app_label=app_label, model=model)
|
||||
return list(ContentType.objects.filter(ct_filter).values_list('pk', flat=True))
|
||||
return super().prepare_value(value)
|
||||
|
||||
|
||||
#
|
||||
# Expansion fields
|
||||
#
|
||||
|
||||
Reference in New Issue
Block a user