39๐
โ
Itโs confusing when you say โextractโ as in exclude.
The answer is in annotation and aggregation.
http://docs.djangoproject.com/en/dev/topics/db/aggregation/
from django.db.models import Count
Games.objects.annotate(num_players=Count('players')).filter(num_players__gt=10)
4๐
I think you are looking for annotate โ based on example in docs
q = Game.objects.filter(players__isnull=False).annotate(Count('players'))
q[0].players__count #prints first games players
or to filter by count
q = Game.objects.annotate(num_players=Count('players')).filter(num_players__gt=1)
๐คJamesO
- How to avoid creating test database for testing in django?
- Django Rest Framework โ How to nest several fields in a serializer?
- Django: generate download link
Source:stackexchange.com