Fixes: #19996 - Correct dynamic query parameters for IP Address field in Add/Edit Service form (#20040)
Some checks are pending
CI / build (20.x, 3.10) (push) Waiting to run
CI / build (20.x, 3.11) (push) Waiting to run
CI / build (20.x, 3.12) (push) Waiting to run
CodeQL / Analyze (${{ matrix.language }}) (none, actions) (push) Waiting to run
CodeQL / Analyze (${{ matrix.language }}) (none, javascript-typescript) (push) Waiting to run
CodeQL / Analyze (${{ matrix.language }}) (none, python) (push) Waiting to run

* Fixes: #19996 - Correct dynamic query parameters for IP Address field in Add/Edit Service form

* Remove debug and do some cleanup
This commit is contained in:
Daniel Sheppard 2025-08-08 08:52:03 -05:00 committed by GitHub
parent 2d495d4f32
commit 5d7c8318aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -21,7 +21,7 @@ from utilities.forms.rendering import FieldSet, InlineFields, ObjectAttribute, T
from utilities.forms.utils import get_field_value from utilities.forms.utils import get_field_value
from utilities.forms.widgets import DatePicker, HTMXSelect from utilities.forms.widgets import DatePicker, HTMXSelect
from utilities.templatetags.builtins.filters import bettertitle from utilities.templatetags.builtins.filters import bettertitle
from virtualization.models import VMInterface from virtualization.models import VMInterface, VirtualMachine
__all__ = ( __all__ = (
'AggregateForm', 'AggregateForm',
@ -783,10 +783,6 @@ class ServiceForm(NetBoxModelForm):
queryset=IPAddress.objects.all(), queryset=IPAddress.objects.all(),
required=False, required=False,
label=_('IP Addresses'), label=_('IP Addresses'),
query_params={
'device_id': '$device',
'virtual_machine_id': '$virtual_machine',
}
) )
comments = CommentField() comments = CommentField()
@ -815,10 +811,22 @@ class ServiceForm(NetBoxModelForm):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
if (parent_object_type_id := get_field_value(self, 'parent_object_type')): if parent_object_type_id := get_field_value(self, 'parent_object_type'):
try: try:
parent_type = ContentType.objects.get(pk=parent_object_type_id) parent_type = ContentType.objects.get(pk=parent_object_type_id)
model = parent_type.model_class() model = parent_type.model_class()
if model == Device:
self.fields['ipaddresses'].widget.add_query_params({
'device_id': '$parent',
})
elif model == VirtualMachine:
self.fields['ipaddresses'].widget.add_query_params({
'virtual_machine_id': '$parent',
})
elif model == FHRPGroup:
self.fields['ipaddresses'].widget.add_query_params({
'fhrpgroup_id': '$parent',
})
self.fields['parent'].queryset = model.objects.all() self.fields['parent'].queryset = model.objects.all()
self.fields['parent'].widget.attrs['selector'] = model._meta.label_lower self.fields['parent'].widget.attrs['selector'] = model._meta.label_lower
self.fields['parent'].disabled = False self.fields['parent'].disabled = False