group by "project"

13 views
Skip to first unread message

Mohamed Yahiya Shajahan

unread,
May 24, 2023, 9:21:21 PM5/24/23
to Django users
    def list(self, request, *args, **kwargs):
        project_id = self.request.query_params.get('project_id')
        if project_id:
            queryset = RegistrationDatesSlots.objects.values('date').annotate(project=Count('project')).filter(project=project_id)
            # queryset = RegistrationDatesSlots.objects.filter(project=project_id).query.group_by=['project']
        else:
            queryset = RegistrationDatesSlots.objects.all().values('project', 'date')

        serialized_data = []
        for item in queryset:
            serialized_item = {
                'date': item['date'],
                'project': item['project']
            }
            serialized_data.append(serialized_item)
        return Response(serialized_data)


this is my views i want to group by "project" but shows only one record,
i know there are multiple records there



 The content of this email is confidential and intended for the recipient specified in message only. It is strictly forbidden to share any part of this message with any third party, without a written consent of the sender. If you received this message by mistake, please reply to this message and follow with its deletion, so that we can ensure such a mistake does not occur in the future.

SAVE PAPER | Good for your planet | Good for your Business

Muhammad Juwaini Abdul Rahman

unread,
May 24, 2023, 9:35:38 PM5/24/23
to django...@googlegroups.com
Do you realize what 'Count' do?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/016aad73-74fc-49c3-80e7-c8d68ea0a6ddn%40googlegroups.com.

Helly Modi

unread,
May 25, 2023, 2:10:46 AM5/25/23
to Django users
TRY THIS
from django.db.models import Count


def list(self, request, *args, **kwargs):
    project_id = self.request.query_params.get('project_id')
    if project_id:
        queryset = RegistrationDatesSlots.objects.values('project', 'date').annotate(count=Count('project')).filter(project=project_id)
    else:
        queryset = RegistrationDatesSlots.objects.values('project', 'date')


    serialized_data = []
    for item in queryset:
        serialized_item = {
            'date': item['date'],
            'project': item['project']
        }
        serialized_data.append(serialized_item)
    return Response(serialized_data)

Mohamed Yahiya Shajahan

unread,
May 25, 2023, 2:28:51 AM5/25/23
to django...@googlegroups.com
[
    {
        "project": 1,
        "date": "2023-05-22",
        "count": 1
    },
    {
        "project": 1,
        "date": "2023-05-27",
        "count": 1
    }
]


throws this response. i want group by project

    def list(self, request, *args, **kwargs):
        project_id = self.request.query_params.get('project_id')
        if project_id:
            queryset = RegistrationDatesSlots.objects.values('project', 'date').annotate(count=Count('project')).filter(project=project_id)
        else:
            queryset = RegistrationDatesSlots.objects.all().values('project', 'date')
        return Response(queryset)
   


this is the code


Reply all
Reply to author
Forward
0 new messages