Skip to content

Conversation

@pheus
Copy link
Contributor

@pheus pheus commented Oct 30, 2025

Fixes: #19523

Summary
Extend usage-count filtering via counter caches to cover DeviceType, ModuleType, and RackType.

Implementation

  • Add counter‑cached integer fields:
    • device_count on DeviceType
    • module_count on ModuleType
    • rack_count on RackType
  • Expose numeric lookups in REST and GraphQL (e.g., device_count__gte=10, module_count=0, {rack_count: {gt: 2}).
  • Include migration to add fields and backfill counts.
  • Add a test.

Database Changes
Adds integer fields on the three models listed above.

@jeremystretch jeremystretch self-requested a review October 31, 2025 14:48
@jeremystretch jeremystretch marked this pull request as ready for review November 5, 2025 14:06
Copy link
Member

@jeremystretch jeremystretch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work @pheus!

@pheus pheus force-pushed the 19523-add-instance-filter-for-devicetype-and-moduletype-countcachefield branch from cb711da to 50a63be Compare November 12, 2025 20:18
@pheus
Copy link
Contributor Author

pheus commented Nov 12, 2025

Thanks for the thoughtful review and the migration tip - much appreciated!

I’ve added a data migration to prefill device_count and module_count, and included a test to ensure we don’t double‑register the signal receivers. Happy to adjust anything further.

@pheus pheus requested a review from jeremystretch November 12, 2025 20:36
@pheus
Copy link
Contributor Author

pheus commented Nov 12, 2025

I just noticed an earlier FR comment asking to extend this to RackType as well - sorry I missed that. I’m happy to apply the same approach to RackType (counter cache + filters). Would you prefer I add it here or open a follow‑up PR? Thanks again!

@jeremystretch
Copy link
Member

@pheus I think it makes sense to fold that in here as well, since it's a continuation of the same pattern. Thanks!

@pheus pheus force-pushed the 19523-add-instance-filter-for-devicetype-and-moduletype-countcachefield branch from 50a63be to 5eecaa0 Compare November 13, 2025 15:55
Comment on lines -24 to +26
from .devices import Device, Module
from .devices import Device
from .modules import Module
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Unrelated import fix. Happy to drop this if you'd prefer to keep the PR focused.

Comment on lines -512 to +518
def get_available_units(self, u_height=1, rack_face=None, exclude=None, ignore_excluded_devices=False):
def get_available_units(self, u_height=1.0, rack_face=None, exclude=None, ignore_excluded_devices=False):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Unrelated cleanup. Happy to drop this if you'd prefer to keep the PR focused.

@pheus pheus changed the title Closes #19523: Add device_count and module_count filter for DeviceType and ModuleType Closes #19523: Add counter‑cached counts & filters for DeviceType, ModuleType, and RackType Nov 13, 2025
Introduces `device_count`, `module_count` and `rack_count` filters to
enable queries based on the existence and count of the associated
device, module or rack instances.
Updates forms, filtersets, and GraphQL schema to support these filters,
along with tests for validation.

Fixes netbox-community#19523
@pheus pheus force-pushed the 19523-add-instance-filter-for-devicetype-and-moduletype-countcachefield branch from 5eecaa0 to d14e741 Compare November 13, 2025 16:21
@pheus
Copy link
Contributor Author

pheus commented Nov 13, 2025

Thanks! I’ve applied the same approach to RackType as well. This is ready for review now. Happy to adjust anything further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants