Home > Mobile >  How to display the data that available in database using exist() query
How to display the data that available in database using exist() query

Time:08-12

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.

CodePudding user response:

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)

CodePudding user response:

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>

  • Related