Skip to content

Conversation

@jasonyates
Copy link

Fixes #8920

Limits the amount of non-racked devices on Site and Location view to 10 and provides a link to the device list this is pre-filtered to the relevant site or location.

Jason Yates added 2 commits April 7, 2022 08:21
Fixes netbox-community#8920

Limits the amount of non-racked devices on Site and Location view to 10 and provides a link to the device list this is pre-filtered to the relevant site or location.
Fixing issue where Displaying 10 of 10 would appear when it's not required.
position__isnull=True,
parent_bay__isnull=True
).prefetch_related('device_type__manufacturer')
).prefetch_related('device_type__manufacturer').order_by('-pk')[:10]
Copy link
Member

Choose a reason for hiding this comment

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

To avoid duplicating the queryset, I suggest declaring it once and then referencing that to get both the limited set ([:10]) and the total count. You can leave the queryset as-is here:

        nonracked_devices = Device.objects.filter(
            site=instance,
            position__isnull=True,
            parent_bay__isnull=True
        ).prefetch_related('device_type__manufacturer')

And under the template context, do:

            'nonracked_devices': nonracked_devices.order_by('-pk')[:10],
            'total_nonracked_devices_count': nonracked_devices.count(),

Same idea with LocationView below. This helps ensure that any future changes to the queryset aren't missed in the second definition.

Copy link
Author

Choose a reason for hiding this comment

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

Neat. I didn't realise you could do that. Updated FR pushed.

Updating to use a single queryset for both template variables
@jeremystretch jeremystretch merged commit 8781d03 into netbox-community:develop Apr 12, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Limit the number of non-racked devices that are listed on the Site and Location pages

2 participants