mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-20 10:16:42 -06:00
Override get_bound_field() on FilterChoiceFieldMixin to restrict the queryset of bound fields
This commit is contained in:
parent
202a0a0e73
commit
885ea8a4d5
@ -8,6 +8,7 @@ from django import forms
|
|||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.postgres.forms.jsonb import JSONField as _JSONField, InvalidJSONInput
|
from django.contrib.postgres.forms.jsonb import JSONField as _JSONField, InvalidJSONInput
|
||||||
from django.db.models import Count
|
from django.db.models import Count
|
||||||
|
from django.forms import BoundField
|
||||||
from mptt.forms import TreeNodeMultipleChoiceField
|
from mptt.forms import TreeNodeMultipleChoiceField
|
||||||
|
|
||||||
from .choices import unpack_grouped_choices
|
from .choices import unpack_grouped_choices
|
||||||
@ -607,12 +608,26 @@ class FilterChoiceFieldMixin(object):
|
|||||||
kwargs['widget'] = forms.SelectMultiple(attrs={'size': 6})
|
kwargs['widget'] = forms.SelectMultiple(attrs={'size': 6})
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
|
|
||||||
def label_from_instance(self, obj):
|
# def label_from_instance(self, obj):
|
||||||
label = super().label_from_instance(obj)
|
# label = super().label_from_instance(obj)
|
||||||
obj_count = getattr(obj, self.count_attr, None)
|
# obj_count = getattr(obj, self.count_attr, None)
|
||||||
if obj_count is not None:
|
# if obj_count is not None:
|
||||||
return '{} ({})'.format(label, obj_count)
|
# return '{} ({})'.format(label, obj_count)
|
||||||
return label
|
# 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):
|
class FilterChoiceField(FilterChoiceFieldMixin, forms.ModelMultipleChoiceField):
|
||||||
|
Loading…
Reference in New Issue
Block a user