Standardize filtering logic for the parents of recursively-nested models #9338

Closed
opened 2025-12-29 20:48:42 +01:00 by adam · 0 comments
Owner

Originally created by @jeremystretch on GitHub (Mar 8, 2024).

Originally assigned to: @jeremystretch on GitHub.

NetBox version

v3.7.3

Feature type

Change to existing functionality

Proposed functionality

I'm opening this FR to address an inconsistency in the filtering of parent objects of self-recursive models (e.g. regions, site groups, locations, etc.).

For most of these objects, filtering by the parent object will return only the immediate children of the specified parent. For example, filtering for all regions belonging to North America (PK 1) will return only the countries assigned to that continent; it will not include child states or cities within each country.

# Returns immediate children only
GET /api/dcim/regions/?parent_id=1

However, this is not true for locations. Per #6124, filtering locations by a parent location will return all of the parent object's descendants. For example, if dealing with a hierarchy of buildings, floors, and rooms, filtering for a specific building will return both rooms and floors within it.

# Returns *all* descendants in the hierarchy
GET /api/dcim/locations/?parent_id=1

I am proposing a two-part change:

  1. Standardize the behavior of the parent & parent_id filters so that they behave consistently across all nested models. (This will likely mean modifying the current behavior of the location filter, as it would be the smallest breaking change.)
  2. Introducing an alternative filters for all models to provide the complementary filtering behavior (perhaps named ancestor).

Edit: For example, you could then do

GET /api/dcim/regions/?parent_id=1

to return only the immediate children of region 1, OR

GET /api/dcim/regions/?ancestor_id=1

to return all regions within region 1.

Use case

Neither behavior described above is objectively correct or incorrect: Both have valid use cases. However, we should address the inconsistency and prescribe a clear mechanism for achieving either behavior for all relevant models.

Database changes

None expected

External dependencies

None

Originally created by @jeremystretch on GitHub (Mar 8, 2024). Originally assigned to: @jeremystretch on GitHub. ### NetBox version v3.7.3 ### Feature type Change to existing functionality ### Proposed functionality I'm opening this FR to address an inconsistency in the filtering of parent objects of self-recursive models (e.g. regions, site groups, locations, etc.). For most of these objects, filtering by the parent object will return only the immediate children of the specified parent. For example, filtering for all regions belonging to North America (PK 1) will return only the countries assigned to that continent; it will _not_ include child states or cities within each country. ``` # Returns immediate children only GET /api/dcim/regions/?parent_id=1 ``` However, this is not true for locations. Per #6124, filtering locations by a parent location will return _all_ of the parent object's descendants. For example, if dealing with a hierarchy of buildings, floors, and rooms, filtering for a specific building will return both rooms and floors within it. ``` # Returns *all* descendants in the hierarchy GET /api/dcim/locations/?parent_id=1 ``` I am proposing a two-part change: 1. Standardize the behavior of the `parent` & `parent_id` filters so that they behave consistently across all nested models. (This will likely mean modifying the current behavior of the location filter, as it would be the smallest breaking change.) 2. Introducing an alternative filters for all models to provide the complementary filtering behavior (perhaps named `ancestor`). **Edit:** For example, you could then do ``` GET /api/dcim/regions/?parent_id=1 ``` to return only the _immediate_ children of region 1, OR ``` GET /api/dcim/regions/?ancestor_id=1 ``` to return _all_ regions within region 1. ### Use case Neither behavior described above is objectively correct or incorrect: Both have valid use cases. However, we should address the inconsistency and prescribe a clear mechanism for achieving either behavior for all relevant models. ### Database changes None expected ### External dependencies None
adam added the status: acceptedtype: featurebreaking change labels 2025-12-29 20:48:42 +01:00
adam closed this issue 2025-12-29 20:48:42 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#9338