【问题标题】:How to set the incoming object to true or false in Django如何在 Django 中将传入对象设置为 true 或 false
【发布时间】:2022-10-05 00:57:43
【问题描述】:

如果有适合制作的过滤器的数据,我想要做的是写“True”,否则写“False”

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

charts.models.DoesNotExist: ChartSimilar matching query does not exist. 我收到一个错误

【问题讨论】:

标签: django django-models django-views


【解决方案1】:

此处的 got 查询不存在,因为该对象不具有特定的 id,因此这是一个例外。您可以使用 try 来处理它,除了块。

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

【讨论】:

    猜你喜欢
    • 2012-08-22
    • 2019-02-15
    • 1970-01-01
    • 2020-11-23
    • 2020-02-05
    • 1970-01-01
    • 2019-11-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多