Add device_id and virtual_machine_id to ipam.forms.filtersets.ServiceFilterForm #8701

Closed
opened 2025-12-29 20:40:06 +01:00 by adam · 2 comments
Owner

Originally created by @atownson on GitHub (Oct 3, 2023).

Originally assigned to: @abhi1693 on GitHub.

NetBox version

v3.5.7

Feature type

Change to existing functionality

Proposed functionality

I propose to add fields device_id and virtual_machine_id to the ServiceFilterForm class. Currently there's no way to filter a service by the associated device or virtual machine in the web form. The search method also does not include these fields.

image

image

The API does support filtering by device and virtual machine.

This feature request can be accomplished by updating the filtersets.py from:

class ServiceFilterForm(ServiceTemplateFilterForm):
    model = Service
    tag = TagFilterField(model)

to:

class ServiceFilterForm(ServiceTemplateFilterForm):
    model = Service
    fieldsets = (
        (None, ('q', 'filter_id', 'tag')),
        (_('Attributes'), ('protocol', 'port')),
        (_('Device/VM'), ('device_id', 'virtual_machine_id')),
    )
    device_id = DynamicModelMultipleChoiceField(
        queryset=Device.objects.all(),
        required=False,
        label=_('Assigned Device'),
    )
    virtual_machine_id = DynamicModelMultipleChoiceField(
        queryset=VirtualMachine.objects.all(),
        required=False,
        label=_('Assigned VM'),
    )

Result:
image

image

I could handle this PR if desired and if this request is approved.

Use case

Adding these fields would improve the user experience, align the services model filterset with the IP address model filterset, and provide functionality available in the API to the web filter forms.

Database changes

None

External dependencies

None

Originally created by @atownson on GitHub (Oct 3, 2023). Originally assigned to: @abhi1693 on GitHub. ### NetBox version v3.5.7 ### Feature type Change to existing functionality ### Proposed functionality I propose to add fields **device_id** and **virtual_machine_id** to the **ServiceFilterForm** class. Currently there's no way to filter a service by the associated device or virtual machine in the web form. The search method also does not include these fields. ![image](https://github.com/netbox-community/netbox/assets/52260120/d9922cd9-a559-4239-8ac1-019beb559a39) ![image](https://github.com/netbox-community/netbox/assets/52260120/ad4aad53-c3f0-42c3-b2ed-393af2ded4bf) The API does support filtering by device and virtual machine. This feature request can be accomplished by updating the filtersets.py from: ``` class ServiceFilterForm(ServiceTemplateFilterForm): model = Service tag = TagFilterField(model) ``` to: ``` class ServiceFilterForm(ServiceTemplateFilterForm): model = Service fieldsets = ( (None, ('q', 'filter_id', 'tag')), (_('Attributes'), ('protocol', 'port')), (_('Device/VM'), ('device_id', 'virtual_machine_id')), ) device_id = DynamicModelMultipleChoiceField( queryset=Device.objects.all(), required=False, label=_('Assigned Device'), ) virtual_machine_id = DynamicModelMultipleChoiceField( queryset=VirtualMachine.objects.all(), required=False, label=_('Assigned VM'), ) ``` Result: ![image](https://github.com/netbox-community/netbox/assets/52260120/ad43b9bd-6695-40e7-a2d9-46fb55200799) ![image](https://github.com/netbox-community/netbox/assets/52260120/765987c8-ac69-40eb-9d16-02133229b01c) I could handle this PR if desired and if this request is approved. ### Use case Adding these fields would improve the user experience, align the services model filterset with the IP address model filterset, and provide functionality available in the API to the web filter forms. ### Database changes None ### External dependencies None
adam added the status: acceptedtype: feature labels 2025-12-29 20:40:06 +01:00
adam closed this issue 2025-12-29 20:40:06 +01:00
Author
Owner

@atownson commented on GitHub (Oct 5, 2023):

Another option for this would be to modify the string definition of the Service model to include the device/vm:

def __str__(self):
    return f'{self.device or self.virtual_machine} -> {self.name} ({self.get_protocol_display()}/{self.port_list})'

This could obviously get rather lengthy and verbose, depending on the Service properties, but one example might be:

vm01splunk -> Splunk Web (TCP/8000)

This would need to be searchable too.

@atownson commented on GitHub (Oct 5, 2023): Another option for this would be to modify the string definition of the Service model to include the device/vm: ``` def __str__(self): return f'{self.device or self.virtual_machine} -> {self.name} ({self.get_protocol_display()}/{self.port_list})' ``` This could obviously get rather lengthy and verbose, depending on the Service properties, but one example might be: ``` vm01splunk -> Splunk Web (TCP/8000) ``` This would need to be searchable too.
Author
Owner

@jeremystretch commented on GitHub (Nov 6, 2023):

The API does support filtering by device and virtual machine.

This is incorrect. ServiceFilterSet supports filtering by both device and VM by ID or name. The scope of this FR would be limited to adding these fields to the filter form.

@jeremystretch commented on GitHub (Nov 6, 2023): > The API does support filtering by device and virtual machine. This is incorrect. ServiceFilterSet supports filtering by both device and VM by ID or name. The scope of this FR would be limited to adding these fields to the filter form.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#8701