Count और Sum से Aggregation
from django.db.models import Count, Sum
ModelName.objects.filter(field=value).count()
ModelName.objects.aggregate(total=Sum('field'), number=Count('id'))
Rows Count करना
Matching rows count करने का सबसे simple तरीका QuerySet.count() है, जो हर row को Python में load करने के बजाय database से एक number मांगता है।
उदाहरण: Counting Rows
This returns a single integer — the number of published tutorial rows — without loading their data.
from core.models import Tutorial
# How many tutorials are published?
Tutorial.objects.filter(is_published=True).count()
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Count और Sum के साथ aggregate() उपयोग करना
aggregate() पूरी queryset पर एक या ज़्यादा aggregation functions चलाता है और results की एक dictionary return करता है।
उदाहरण: Using aggregate() with Count and Sum
This returns something like {total: 15, total_order: 120} — one dictionary summarizing the whole queryset.
from django.db.models import Count, Sum
from core.models import Chapter
Chapter.objects.aggregate(
total=Count('id'),
total_order=Sum('order'),
)
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
Related Objects Count करना
Count यह भी count कर सकता है कि हर row के कितने related objects हैं, जो 'इस chapter में कितनी tutorials हैं' दिखाने के लिए उपयोगी है।
उदाहरण: Counting Related Objects
Each Chapter object in the result now has a .tutorial_count attribute showing how many related Tutorial rows point to it.
from django.db.models import Count
from core.models import Chapter
Chapter.objects.annotate(tutorial_count=Count('tutorials'))
{# Django-only code -- models.py/views.py/urls.py/settings.py
snippets, or template markup using Django template tags/variables
-- can't run standalone via Judge0 or the browser preview, since
it needs a real Django project. Only this course's pure-Python
examples (example_lang == 'python', no Django imports) are
actually runnable, so those still get the button below. #}
- .count() (एक QuerySet method जो एक number return करता है) को Count (एक aggregation function जो .aggregate() या .annotate() के अंदर उपयोग होता है) के साथ confuse करना।
- Count/Sum को django.db.models से उपयोग करने से पहले import करना भूल जाना।
- aggregate() उपयोग करना जब annotate() चाहिए था (या इसके उल्टा) — aggregate() पूरी queryset के लिए एक summary return करता है, annotate() हर object में एक value जोड़ता है।
- aggregate() पूरी queryset पर एक single summary value compute करता है।
- Count और Sum django.db.models से import होते हैं और aggregate() के अंदर उपयोग होते हैं।
- annotate() एक overall summary के बजाय queryset में हर object में एक computed value जोड़ता है।
Chapter Quiz — Complete all 9 topics to unlock
0/9 topics done
Complete these topics first: