Skip to content

Commit f649b9f

Browse files
committed
Fixes #3106: Restrict queryset of chained fields when form validation fails
1 parent 5caa04e commit f649b9f

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

docs/release-notes/version-2.6.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
## Bug Fixes
1010

11+
* [#3106](https://github.com/netbox-community/netbox/issues/3106) - Restrict queryset of chained fields when form validation fails
1112
* [#3695](https://github.com/netbox-community/netbox/issues/3695) - Include A/Z termination sites for circuits in global search
1213
* [#3712](https://github.com/netbox-community/netbox/issues/3712) - Scrolling to target (hash) did not account for the header size
1314
* [#3780](https://github.com/netbox-community/netbox/issues/3780) - Fix AttributeError exception in API docs

netbox/utilities/forms.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -701,16 +701,22 @@ def __init__(self, *args, **kwargs):
701701
else:
702702
break
703703

704+
# Limit field queryset by chained field values
704705
if filters_dict:
705706
field.queryset = field.queryset.filter(**filters_dict)
707+
# Editing an existing instance; limit field to its current value
706708
elif not self.is_bound and getattr(self, 'instance', None) and hasattr(self.instance, field_name):
707709
obj = getattr(self.instance, field_name)
708710
if obj is not None:
709711
field.queryset = field.queryset.filter(pk=obj.pk)
710712
else:
711713
field.queryset = field.queryset.none()
712-
elif not self.is_bound:
714+
# Creating a new instance with no bound data; nullify queryset
715+
elif not self.data.get(field_name):
713716
field.queryset = field.queryset.none()
717+
# Creating a new instance with bound data; limit queryset to the specified value
718+
else:
719+
field.queryset = field.queryset.filter(pk=self.data.get(field_name))
714720

715721

716722
class ReturnURLForm(forms.Form):

0 commit comments

Comments
 (0)