Home > Net >  How to set the incoming object to true or false in Django
How to set the incoming object to true or false in Django

Time:09-26

What I want to do is write "True" if there is data suitable for the filters made, otherwise "False"

if ChartSimilar.objects.get(chart=pk, year=year):
   print('True')
else:
   print('False')

charts.models.DoesNotExist: ChartSimilar matching query does not exist. I get an error

CodePudding user response:

====== try with this code snippet =====

here got query does not exist because the object is not available with a particular id so it's an exception. you can handle it by using try, except block.

try:
    ChartSimilar.objects.get(chart=pk, year=year)
    print('True')
except:
   print('False') 
  • Related