'>' не поддерживается между экземплярами 'str' и 'float'
Я использую оператор 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. Действительно, x or y возвращает x, если истинность x равна True и y в противном случае.
Вы можете работать с:
if batch == None or batch == 0:
# …
или короче:
if batch in (None, 0):
# …
Кроме того, ваш current_acerage - это strинг, а не float, поэтому вы можете провести сравнение с:
if batch_acerage in (None, 0) and float(current_acerage) > farm_acerage:
return Response(
{"error": True, "message": "Ckkannot add batch more than farm capacity"},
status = status.HTTP_200_OK
)