From 885ea8a4d5f7191afd564501e3b013b1c9ff85f6 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Fri, 7 Feb 2020 18:04:40 -0500 Subject: [PATCH] Override get_bound_field() on FilterChoiceFieldMixin to restrict the queryset of bound fields --- netbox/utilities/forms.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/netbox/utilities/forms.py b/netbox/utilities/forms.py index 355484673..28b906140 100644 --- a/netbox/utilities/forms.py +++ b/netbox/utilities/forms.py @@ -8,6 +8,7 @@ from django import forms from django.conf import settings from django.contrib.postgres.forms.jsonb import JSONField as _JSONField, InvalidJSONInput from django.db.models import Count +from django.forms import BoundField from mptt.forms import TreeNodeMultipleChoiceField from .choices import unpack_grouped_choices @@ -607,12 +608,26 @@ class FilterChoiceFieldMixin(object): kwargs['widget'] = forms.SelectMultiple(attrs={'size': 6}) super().__init__(*args, **kwargs) - def label_from_instance(self, obj): - label = super().label_from_instance(obj) - obj_count = getattr(obj, self.count_attr, None) - if obj_count is not None: - return '{} ({})'.format(label, obj_count) - return label + # def label_from_instance(self, obj): + # label = super().label_from_instance(obj) + # obj_count = getattr(obj, self.count_attr, None) + # if obj_count is not None: + # return '{} ({})'.format(label, obj_count) + # return label + + def get_bound_field(self, form, field_name): + + bound_field = BoundField(form, self, field_name) + + # Modify the QuerySet of the field before we return it. Limit choices to any data already bound: Options + # will be populated on-demand via the APISelect widget. + if bound_field.data: + kwargs = {'{}__in'.format(self.to_field_name or 'pk'): bound_field.data} + self.queryset = self.queryset.filter(**kwargs) + else: + self.queryset = self.queryset.none() + + return bound_field class FilterChoiceField(FilterChoiceFieldMixin, forms.ModelMultipleChoiceField):