Standardize approach for counting related object in object lists #3868

Closed
opened 2025-12-29 18:31:41 +01:00 by adam · 3 comments
Owner

Originally created by @jeremystretch on GitHub (Jul 17, 2020).

Originally assigned to: @jeremystretch on GitHub.

Proposed Changes

Identify and employ a standardized approach for all instances where each object within a list must include a count of one or more related objects. In some cases, we use Count(), but in others we use subqueries. We should pick one and ensure a consistent approach is used throughout the application.

Justification

Provides consistency in queryset definitions and avoids bugs that might result from divergent implementations.

Originally created by @jeremystretch on GitHub (Jul 17, 2020). Originally assigned to: @jeremystretch on GitHub. ### Proposed Changes Identify and employ a standardized approach for all instances where each object within a list must include a count of one or more related objects. In some cases, we use `Count()`, but in others we use subqueries. We should pick one and ensure a consistent approach is used throughout the application. ### Justification Provides consistency in queryset definitions and avoids bugs that might result from divergent implementations.
adam added the status: under reviewtype: housekeeping labels 2025-12-29 18:31:41 +01:00
adam closed this issue 2025-12-29 18:31:41 +01:00
Author
Owner

@jeremystretch commented on GitHub (Jul 17, 2020):

Marking this as blocked until we have a better idea whether we can support cache invalidation for subqueries (see Suor/django-cacheops#365).

@jeremystretch commented on GitHub (Jul 17, 2020): Marking this as blocked until we have a better idea whether we can support cache invalidation for subqueries (see Suor/django-cacheops#365).
Author
Owner

@jeremystretch commented on GitHub (Jul 20, 2020):

While it looks like we may be able to work on this limitation upstream, we won't be able to do that in time for the v2.9 beta release. I'd really prefer to move to Django 3.1 now; if we wait, it'll have to be until the v2.10 release much later this year.

For now, we can append a manual order_by() method to the affected querysets to preserve the existing ordering. This can be reversed in the future once we've secured caching support for subqueries.

@jeremystretch commented on GitHub (Jul 20, 2020): While it looks like we may be able to work on this limitation upstream, we won't be able to do that in time for the v2.9 beta release. I'd really prefer to move to Django 3.1 now; if we wait, it'll have to be until the v2.10 release much later this year. For now, we can append a manual `order_by()` method to the affected querysets to preserve the existing ordering. This can be reversed in the future once we've secured caching support for subqueries.
Author
Owner

@lampwins commented on GitHub (Oct 25, 2020):

@jeremystretch now that cacheops 5.1.0 has been released I assume our stance on this will be to always use subqueries?

@lampwins commented on GitHub (Oct 25, 2020): @jeremystretch now that cacheops 5.1.0 has been released I assume our stance on this will be to always use subqueries?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/netbox#3868