Skip to content

Instantly share code, notes, and snippets.

@kezabelle
Created August 2, 2016 14:10
Show Gist options
  • Save kezabelle/f9bd118088a088162873965dad3a4eca to your computer and use it in GitHub Desktop.
Save kezabelle/f9bd118088a088162873965dad3a4eca to your computer and use it in GitHub Desktop.
Get the queryset with the most results, for blackcross in #django IRC
querysets = (
Page.objects.filter(...),
Something.objects.exclude(...),
Etc.objects.filter(...),
)
results = {qs.count(): (index, qs) for index, qs in enumerate(querysets)}
best = max(results)
worst = min(results)
if best > 0:
best_counts = results[best][1]
if worst > 0:
worst_counts = results[worst][1]
# note you can also figure out what index gave you the best results by looking at [0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment