skip to Main Content

I have this Code. But I need to display the values that exists in the database, Not like "It’s Exists in the Database", I need to display the values of that given email_id.

def post(request): 
if request.method == 'GET':
    value = example.objects.all()
    email_id = request.GET.get('email_id', None)      
    if example.objects.filter(email_id = email_id).exists():
        
         tutorials_serializer = exampleSerializer(value, many=True)
         return JsonResponse(tutorials_serializer.data, safe=False)

Pls help me through it.

2

Answers


  1. You could try this:

    def post(request): 
        if request.method == 'GET':
            value = example.objects.all()
            email_id = request.GET.get('email_id', None)
            if email_id: 
                queryset = example.objects.filter(email_id = email_id)
                if queryset.exists():
                    tutorials_serializer = exampleSerializer(queryset, many=True)
                    return JsonResponse(tutorials_serializer.data, safe=False)
    
    Login or Signup to reply.
  2. You’re almost there – You need to filter your values, not just check there is something in the filtered qs.

    def post(request): 
        if request.method == 'GET':
            email_id = request.GET.get('email_id', None)      
            values = example.objects.filter(email_id=email_id)
            if values.exists():
                tutorials_serializer = exampleSerializer(value, many=True)
                return JsonResponse(tutorials_serializer.data, safe=False)
            return <Some default response that you want when nothing exists>
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search