Skip to content

Instantly share code, notes, and snippets.

@morenoh149
Created October 6, 2022 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save morenoh149/70f584c6e772a78af98104fb4effaa1c to your computer and use it in GitHub Desktop.
Save morenoh149/70f584c6e772a78af98104fb4effaa1c to your computer and use it in GitHub Desktop.
django orm optimization count on aggregates
results = myModel.objects.values().annotate(
group_representative=ArrayAggFirstElem('pk', distinct=True),
)
group_representatives = results.values_list('group_representative', flat=True)
assert_(
results.count() == group_representatives.count(),
'Aggregation Spec should partition the results space (no overlaps)'
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment