[Answer]-SQL query issue in Django

1👍

You didn’t call the method:

result = cursor.fetchall()

However you shouldn’t really be using raw SQL here. Use the model layer:

result = ModelCombinationBank.objects.filter(validity=1, combination=combination).count()

assuming your model is called ModelCombinationBank. And if all you need to is to check that the combination exists, use exists() instead of count(), since that is a cheaper query.

0👍

Another way to see if a value exists and do something if it does:

try:
    obj = ModelCombinationBank.objects.get(validity=1, combination=combination)
    # do something with obj
except ModelCombinationBank.DoesNotExist:
    # do something if not found

Leave a comment