Home > Software engineering >  How to get query parameter through form-data postman in django
How to get query parameter through form-data postman in django

Time:08-24

I have this viewset which should return certian results when i give query param week, month or year.

class TotalOrdersViewset(viewsets.ModelViewSet):
    queryset = Order.objects.all()
    serializer_class = OrderSerializer
    def list(self, request, *args, **kwargs):
        try:
            type = request.data.get('type')
            print('type= ' str(type))
            if type is None:
                return Response({"success": False, "message": "type param describing month or week is missing."}, status=400)
            if type == 'month':
                qs = Order.objects.filter(date__month=datetime.now().month).count()
                return Response({'Number of orders this month': qs})
            elif type == 'year':
                qs = Order.objects.filter(date__year=datetime.now().year).count()
                return Response({'Number of orders this year': qs})
            elif type == 'week':
                TODAY = date.today()
                start = TODAY - timedelta(days=TODAY.weekday())
                end = start   timedelta(days=6)
                qs = Order.objects.filter(date__range=(start, end)).count()
                return Response({'Number of order this week': qs})
            else:
                return Response({})
        except Exception as e:
            return Response({'error':str(e)})

enter image description here

I am specifying parameter but request.data.get is not accessing it.

I have also tried request.session.get and request.POST.get.

Can somebody tell me where I am wrong?

CodePudding user response:

I ran your code my machine and it's working fine.

from datetime import datetime                                                   
from django.shortcuts import render                                             
from rest_framework import viewsets                                                
from rest_framework.response import Response                                       
from user.models import Order                                                   
                                                                               
class TotalOrdersViewset(viewsets.ModelViewSet):                                   
    queryset = Order.objects.all()                                               
    def list(self, request, *args, **kwargs):                                      
    try:                                                                       
        type = request.data.get('type')                                        
        print('type= ' str(type))                                              
        if type is None:                                                       
            return Response({"success": False, "message": "type param describing month or week is missing."}, status=400)
        if type == 'month':                                                    
            qs = Order.objects.filter(date__month=datetime.now().month).count()
            return Response({'Number of orders this month': qs})               
        elif type == 'year':                                                   
            qs = Order.objects.filter(date__year=datetime.now().year).count()
            return Response({'Number of orders this year': qs})                
        elif type == 'week':                                                   
            TODAY = date.today()                                               
            start = TODAY - timedelta(days=TODAY.weekday())                    
            end = start   timedelta(days=6)                                    
            qs = Order.objects.filter(date__range=(start, end)).count()        
            return Response({'Number of order this week': qs})                 
        else:                                                                  
            return Response({})                                                
    except Exception as e:                                                     
        return Response({'error':str(e)}) 

Can you please describe your issue a bit more.

NOTE: Please do not use type as a variable in python code because it is a built in keyword.

enter image description here

  • Related