-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Closes #19523: Add instance count filter to Module and Device Types #20292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Closes #19523: Add instance count filter to Module and Device Types #20292
Conversation
993e985
to
c8d4d85
Compare
Thanks for the thoughtful review and patience. I’ve updated the PR to support querying by instance count. While working on the GraphQL side I used Would it be useful to migrate other numeric counters (e.g., One question (happy to move to a discussion if you’d prefer): booleans in GraphQL are exposed as Happy to update the PR either way. |
c8d4d85
to
c860bff
Compare
Thanks @pheus, it's clear you've put a lot of work into this. One thing to always keep in mind when working on a new feature is to ask "Does this approach scale?" In this case, for instance, what if we had to add the same filters to ten other models? Or 100 other models? That would introduce a ton of boilerplate, which would be impractical to maintain long-term. There are a few things we can do to simplify the implementation here. First, I want to point out that we typically don't explicitly define filters for individual lookups ( Next, if we look at the UI view for the DeviceType list, we can see that Lines 1105 to 1108 in 192440a
Our Now
We could convert this to a normal read-only field and move the annotation to the base queryset to ensure the filter always works with the REST API. I'm not sure offhand what the preferred approach would be for the GraphQL API, but presumably we have a pattern elsewhere for handling this sort of field. (And if not, that in itself might be something worth exploring.) We should also consider whether there's a need for an explicit boolean filter. In this case, |
First of all, thank you so much for taking the time to review the code and explain all these details to me. This is really helpful. I really appreciate it! 💟 Second, the more I dig into the inner workings of NetBox, the more impressed I am by how much thoughtful helper code lives in the background to make per‑model implementations straightforward. I completely understand your point about reducing duplication and boilerplate. I had assumed this might be an edge case without a more generic solution. I’ll take the time to read through everything and work out a cleaner, simplified approach. I know it takes a lot of time to review code and write such thorough explanations, so thank you again for all of this! |
c860bff
to
b8c0bca
Compare
Introduces `instance_count` filters to enable queries based on the existence and count of the associated device or module instances. Updates forms, filtersets, and GraphQL schema to support these filters.
b8c0bca
to
c66b9f8
Compare
Thanks for your patience! I tried to find a common NetBox pattern that would let a FilterSet reuse the view‑level annotation. Since FilterSets operate on model fields or custom methods, I couldn’t find a clean way to say “this field is already annotated.” As a stop‑gap, I added an As an alternative, I explored a Naming: do you prefer I may be missing an obvious approach here. Any guidance on the preferred direction (reuse annotation vs. small helper vs. counter cache) would be really appreciated. And if this FR is creating more work than it saves, I’m happy to step back. |
Fixes: #19523
Summary
Add filters to quickly find used/unused types and query by count:
has_instances
(bool) — UI, REST, GraphQLinstance_count
(int with=, gt, gte, lt, lte, range
) — REST, GraphQLImplementation
DeviceTypeFilterSet
&ModuleTypeFilterSet
: addhas_instances
andinstance_count
usingcount_related()
; a single method handles integer lookups.@filter_field
; annotateinstance_count
and delegate lookups withprocess_filters(prefix="…instance_count__")
./api/dcim/device-types/?has_instances=true
/api/dcim/device-types/?instance_count__gte=10
/api/dcim/module-types/?has_instances=false