django count code example
Example 1: count gabarit django
{{ posts|length }}
Example 2: count gabarit django
def places(request):
places = Places.objects.order_by('-published_date')[:10]
places_count = Places.objects.count()
return render(
request, 'templates/places.html', {'places':places, 'places_count': places_count}
)
<div class="container">
<h2>Places <span class="badge">{{ places_count }}</span></h2>
</div>
Example 3: django group by
from django.db.models import Count
Members.objects.values('designation').annotate(dcount=Count('designation'))
SELECT designation, COUNT(designation) AS dcount
FROM members GROUP BY designation
[{'designation': 'Salesman', 'dcount': 2},
{'designation': 'Manager', 'dcount': 2}]
Example 4: django filter values with OR operator
Blog.objects.filter(pk__in=[1, 4, 7])
Example 5: django 3.0 queryset examples
>>> Entry.objects.filter(
... headline__startswith='What'
... ).exclude(
... pub_date__gte=datetime.date.today()
... ).filter(
... pub_date__gte=datetime.date(2005, 1, 30)
... )
Example 6: django queryset count
Entry.objects.count()
Entry.objects.filter(headline__contains='Lennon').count()