Unable to filter Sites by multiple Regions #2286

Closed
opened 2025-12-29 17:24:30 +01:00 by adam · 2 comments
Owner

Originally created by @jbakklund on GitHub (Jan 14, 2019).

Environment

  • Python version: 3.5.3
  • NetBox version: 2.5.3

Steps to Reproduce

  1. Make sure that you have entered Sites for multiple regions like for example Africa, America, Asia, Australia, and Europe.
  2. In the Sites view Search/Filet box, select multiple (two or more) Regions and click Apply.

Expected Behavior

You would expect NetBox to be able to show all Sites in the selected Regions, in the same way as if you select to filter Sites by e.g. multiple Tenants.

Observed Behavior

The resulting list shows only Sites for the alphabetically last one of the Regions selected in the filter box.

Originally created by @jbakklund on GitHub (Jan 14, 2019). ### Environment * Python version: 3.5.3 * NetBox version: 2.5.3 ### Steps to Reproduce 1. Make sure that you have entered Sites for multiple regions like for example Africa, America, Asia, Australia, and Europe. 2. In the Sites view Search/Filet box, select multiple (two or more) Regions and click Apply. <!-- What did you expect to happen? --> ### Expected Behavior You would expect NetBox to be able to show all Sites in the selected Regions, in the same way as if you select to filter Sites by e.g. multiple Tenants. <!-- What happened instead? --> ### Observed Behavior The resulting list shows only Sites for the alphabetically last one of the Regions selected in the filter box.
adam added the type: bugstatus: accepted labels 2025-12-29 17:24:30 +01:00
adam closed this issue 2025-12-29 17:24:30 +01:00
Author
Owner

@DanSheps commented on GitHub (Jan 14, 2019):

I can duplicate this on v2.5.3. Can confirm it is the last one as well.

@DanSheps commented on GitHub (Jan 14, 2019): I can duplicate this on v2.5.3. Can confirm it is the last one as well.
Author
Owner

@DanSheps commented on GitHub (Feb 27, 2019):

I have been looking into this a bit. Due to the way the current method works to filter, it returns no query objects if a region is invalid.

We can convert to use ModelMultipleChoiceFilter, however there is a change we need to consider. Due to the way TreeNodeChoiceField (the form object) cleans the data, if it encounters an invalid region, it just short-circuits and returns the full queryset for filtering. This is the way it works with device filter or even site filter with the status field (not a TreeNodeChoiceField but a MultipleChoice field).

This won't be damaging for the web interface, as you can't choose invalid data anyways, however in the API it will return the full queryset.

As mentioned, any other filter that uses ModelMultipleChoiceFilter will behave this way, and I am thinking we want to be consistent with the behaviour and the best bet would be to extend ModelMultipleChoiceFilter to "TreeNodeMultipleChoiceFilter" and override filter() to perform a get_descendants query before going to the parent.

This would allow handling of things like NestedRackGroups or anything else that uses the MPTT model.

Thoughts?

@DanSheps commented on GitHub (Feb 27, 2019): I have been looking into this a bit. Due to the way the current method works to filter, it returns no query objects if a region is invalid. We can convert to use ModelMultipleChoiceFilter, however there is a change we need to consider. Due to the way TreeNodeChoiceField (the form object) cleans the data, if it encounters an invalid region, it just short-circuits and returns the full queryset for filtering. This is the way it works with device filter or even site filter with the status field (not a TreeNodeChoiceField but a MultipleChoice field). This won't be damaging for the web interface, as you can't choose invalid data anyways, however in the API it will return the full queryset. As mentioned, any other filter that uses ModelMultipleChoiceFilter will behave this way, and I am thinking we want to be consistent with the behaviour and the best bet would be to extend ModelMultipleChoiceFilter to "TreeNodeMultipleChoiceFilter" and override filter() to perform a get_descendants query before going to the parent. This would allow handling of things like NestedRackGroups or anything else that uses the MPTT model. Thoughts?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#2286