UI filter forms should filter by numeric ID rather than by slug #4606

Closed
opened 2025-12-29 18:38:11 +01:00 by adam · 0 comments
Owner

Originally created by @jeremystretch on GitHub (Feb 27, 2021).

Originally assigned to: @jeremystretch on GitHub.

Proposed Changes

Currently, when filtering a list of objects within the NetBox web UI, many of the provided search fields apply a query filter based on the selected object's slug. For example, filtering for all devices in a site with the slug raleigh4 would apply the query parameter ?site=raleigh-4.

This issue proposes changing all such fields to reference the related object by its numeric ID instead. For example, the above query would become ?site_id=123, where 123 is the site's primary key.

To be clear, this does not change the behavior of the actual filters, and will not have any effect on external consumers of the REST API. It merely changes which of the existing filters NetBox's own UI uses.

Justification

Although more human-friendly, filtering by a related object's slug field requires a SQL JOIN when making the database query to pull in the related object:

SELECT DISTINCT ...
  FROM "dcim_device"
 INNER JOIN "dcim_site"
    ON ("dcim_device"."site_id" = "dcim_site"."id")
 WHERE "dcim_site"."slug" = 'raleigh4'
 ORDER BY "dcim_device"."_name" ASC, "dcim_device"."id" ASC

Filtering by the related object's ID does not require a JOIN, because that value is stored in the filtered object's foreign key field.

Originally created by @jeremystretch on GitHub (Feb 27, 2021). Originally assigned to: @jeremystretch on GitHub. ### Proposed Changes Currently, when filtering a list of objects within the NetBox web UI, many of the provided search fields apply a query filter based on the selected object's slug. For example, filtering for all devices in a site with the slug `raleigh4` would apply the query parameter `?site=raleigh-4`. This issue proposes changing all such fields to reference the related object by its numeric ID instead. For example, the above query would become `?site_id=123`, where 123 is the site's primary key. To be clear, this does **not** change the behavior of the actual filters, and will not have any effect on external consumers of the REST API. It merely changes _which_ of the existing filters NetBox's own UI uses. ### Justification Although more human-friendly, filtering by a related object's slug field requires a SQL JOIN when making the database query to pull in the related object: ``` SELECT DISTINCT ... FROM "dcim_device" INNER JOIN "dcim_site" ON ("dcim_device"."site_id" = "dcim_site"."id") WHERE "dcim_site"."slug" = 'raleigh4' ORDER BY "dcim_device"."_name" ASC, "dcim_device"."id" ASC ``` Filtering by the related object's ID does not require a JOIN, because that value is stored in the filtered object's foreign key field.
adam added the status: acceptedtype: housekeeping labels 2025-12-29 18:38:11 +01:00
adam closed this issue 2025-12-29 18:38:11 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#4606