0

I am trying to count the number of users in my event name field. But I am facing problems in django-rest-framework filtering.

I tried different logics, but I cannot count. I got the count when I hard coded the event name in my views.py file, but I don't want to hard code it in the views. Either I can extract the event name from the url or a logic, which can simply take event name and give me the count of the user in it.

This is my models.py:

from django.db import models

# Create your models here.

class FormDetails(models.Model):
    event_name = models.CharField(max_length=100)
    page_index = models.CharField(max_length = 10)
    device_choices = (
         ('mobile','mobile'),
         ('PC','PC'),
         ('tablet','tablet'),
    )
    device = models.CharField(choices=device_choices,max_length=15)
    metadata = models.TextField()
    session_data = models.TextField()
    ip_address = models.CharField(max_length = 30, null=True,blank=True)
    time = models.DateTimeField(auto_now_add=True)
    user_unique_identifier = models.BigIntegerField()
    status_choice = ( 
        ('complete','complete'),
    )
    status = models.CharField(max_length=20,null=True,blank=True, choices=status_choice)
    page_name = models.CharField(max_length=50, null=True,blank=True)
    button_name = models.CharField(max_length=20,null=True,blank=True)

This is my serializers.py:

from rest_framework import serializers
from .models import FormDetails

class FormSerializer(serializers.Serializer):
    class Meta(object):
        model = FormDetails
    
    fields = ['event_name','page_index','device','metadata','session_data','ip_address','time','user_unique_identifier','status','page_name','button_name']

    def get_accounts_items(self, obj):
        total_count = FormDetails.objects.filter(
        user_unique_identifier=obj.id)
        serializer = FormSerializer(total_count, many=True)
        return serializer.data

This is my views.py:

from django.shortcuts import render
from .models import FormDetails
from .serializers import FormSerializer
from rest_framework import viewsets
from rest_framework.response import Response
from rest_framework.renderers import JSONRenderer
from rest_framework.permissions import AllowAny
from rest_framework.decorators import action
from rest_framework.response import Response
  
class FormViewSet(viewsets.ModelViewSet):
    http_method_names = ['get','post', 'patch', 'head', 'options', 'put', 'delete']
    queryset = FormDetails.objects.all()
    serializer_class = FormSerializer
    permission_classes = [AllowAny]
    filter_backends = [django_filters.rest_framework.DjangoFilterBackend]
 
    @action(detail=False)
    def ipcount(self, request):
        count = FormDetails.objects.exclude(ip_address=None).count()
        content = {
            'ip_count': count
        }
        return Response(content)  

    @action(detail=False)
    def devicecount(self, request):
        count = FormDetails.objects.exclude(device=None).count()
        content = {
            'device_count': count
        }
        return Response(content)
 
    @action(detail=False)
    def pagecount(self, request):
        count = FormDetails.objects.exclude(page_index=None).count()
        content = {
            'page_index_count': count
        }
        return Response(content)

    @action(detail=False)
    def successform(self, request):
        count = FormDetails.objects.filter(status=None).count()
        content = {
            'successfully_filled_count': count
        }
        return Response(content)
 
    @action(detail=False)
    def unsuccessform(self, request):
        count = FormDetails.objects.exclude(status=None).count()
        content = {
            'unsuccessfully_filled_count': count
        }
        return Response(content)

    # @action(detail=False)
    def userbyevent(self, request):
        queryset = super().get_queryset()
        event = self.request.query_params.get('event_name')
        if event == None:
            pass
 
    # @action(detail=False)
    def userbyeyeevent(self, request):
        count = FormDetails.objects.filter(event_name='Eye Care').count()
        content = {
            # 'Eye users are': count
        }
        return Response(content)
  
    # @action(detail=False)
    def userbyhealthevent(self, request): 
        count = FormDetails.objects.filter(event_name='Health Checkup').count()
        content = {
            # 'Health users are': count
        }
        return Response(content)

    # @action(detail=False)
    def userbydentalevent(self, request): 
        count = FormDetails.objects.filter(event_name='Dental Care').count()
        content = {
            # 'Dental users are': count
        }
        return Response(content)
1
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. Commented Dec 5, 2022 at 8:06

1 Answer 1

0
from rest_framework import serializers
from .models import FormDetails


class FormSerializer(serializers.Serializer):
    class Meta(object):
        model = FormDetails

    fields = [
        "event_name",
        "page_index",
        "device",
        "metadata",
        "session_data",
        "ip_address",
        "time",
        "user_unique_identifier",
        "status",
        "page_name",
        "button_name",
    ]

    def get_accounts_items(self, obj):
        total_count = FormDetails.objects.filter(user_unique_identifier=obj.id)
        serializer = FormSerializer(total_count, many=True)
        return serializer.data
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.