django - '>' not supported between instances of ' str'和 'float'

标签 django if-statement nonetype or-condition

我是 if 使用带有 or 条件的 if 语句 但这显示错误 首先我错了没有和或条件

print(farm_acerage)
print(batch_acerage)
print(current_acerage)


if farm_acerage in (None, 0):
   return Response({"error": True, "message": "Add farm first"},
                   status = status.HTTP_200_OK)

if farm_acerage is not None and batch_acerage in (None, 0):
   if current_acerage > farm_acerage:
      return Response({"error": True, "message": "Ckkannot add batch more than farm capacity"},
                      status = status.HTTP_200_OK)

if farm_acerage is not None and batch_acerage is not None:
   batch_acerage = float(batch_acerage) + float(current_acerage)
   if batch_acerage > farm_acerage:
      return Response({"error": True, "message": "Cannot add batch more than farm capacity"},
                      status=status.HTTP_200_OK)
                        

错误是

2.0
None
1.0
'>' not supported between instances of 'str' and 'float'

最佳答案

None or 0将返回0 。确实,<em>x</em> or <em>y</em>返回 x 如果 x 的真实性True y 否则。

您可以使用:

if batch <strong>== None</strong> or batch <strong>== 0</strong>:
   # …

或更短:

if batch <strong>in</strong> (None, 0):
    # …

此外,您的current_aceragestr ing,不是float ,因此您可以与以下内容进行比较:

if batch_acerage in (None, 0) and <strong>float(</strong>current_acerage<strong>)</strong> > farm_acerage:
    return Response(
        {"error": True, "message": "Ckkannot add batch more than farm capacity"},
        status = status.HTTP_200_OK
    )

关于django - '>' not supported between instances of ' str'和 'float',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71083492/

相关文章:

java - 使用数组获取正数

string - 根据 Pandas 中其他两列的条件创建一个列

Python 在解析表时处理 NoneType

python - “NoneType”对象没有属性 'earlierDate_'

django - Nginx,Varnish和Django的多个域都返回相同的缓存页面

python - Django - 代理模型的权限

django - 如何清除/维护 django-sentry 数据库?

django - 如何在 Django block 中包含模板标签

javascript - 在 JavaScript 中跳出 if block 的正确方法?

python - matplotlib:以 None 作为值绘制 numpy 数组