0

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

sohretaga
  • 69
  • 1
  • 9
  • Does this answer your question? [Django check for any exists for a query](https://stackoverflow.com/questions/2690521/django-check-for-any-exists-for-a-query) – Abdul Aziz Barkat Sep 25 '22 at 11:14
  • 1
    with *objects.get* you have to use try block https://stackoverflow.com/a/16288605/9632638 to don't get any error like that – K.D Sep 25 '22 at 11:16

1 Answers1

1

Here the 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') 
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313