Wednesday, May 24, 2023

Re: group by "project"

[
    {
        "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


On Thu, May 25, 2023 at 11:41 AM Helly Modi <hellymodi02@gmail.com> wrote:
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)

On Thursday, May 25, 2023 at 7:05:38 AM UTC+5:30 Muhammad Juwaini Abdul Rahman wrote:
Do you realize what 'Count' do?

On Thu, 25 May 2023 at 09:20, 'Mohamed Yahiya Shajahan' via Django users <django...@googlegroups.com> wrote:
    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

--
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.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/28b321b8-4e3b-4fee-a94b-177ab1ba76f0n%40googlegroups.com.


 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

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAAw%3DCNizUd266rbGVJWCemSYkQ%3DJmaVuHCR%2BSFCY03L-1UnO_g%40mail.gmail.com.

No comments:

Post a Comment