'NoneType' 和 'float' 错误 - 如何避免 NoneType

'NoneType' and 'float' Error - How to avoid NoneType

我已经尝试构建我的代码,因此如果 NoneType 是一个结果,它将被转换为 0。但是,我似乎仍然 None 进入下一个函数。这是为什么?

第 321 行错误,在 get_profit_loss_value_fees

result = self.get_profit_loss_value() - self.get_fees()

TypeError:- 的操作数类型不受支持:'NoneType' 和 'float'

def get_profit_loss_value(self):
    if self.get_exit_cpu() > 0:
        if self.type == 'Long':
            result = self.get_entries().aggregate(
            get_profit_loss_value=Sum('amount', output_field=models.FloatField()
                                           ) * (self.get_exit_cpu() - self.get_entry_cpu()))['get_profit_loss_value']
            return 0 if result is None else result
        elif self.type == 'Short':
            ...
    else:
        return 0

def get_profit_loss_value_fees(self):
    result = self.get_profit_loss_value() - self.get_fees()
    return result

根据要求整个回溯:

Traceback (most recent call last):
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\core\handlers\base.py", line 145, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\core\handlers\base.py", line 143, in _get_response
    response = response.render()
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\response.py", line 105, in render
    self.content = self.rendered_content
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\response.py", line 83, in rendered_content
    return template.render(context, self._request)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 171, in render
    return self._render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 163, in _render
    return self.nodelist.render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\loader_tags.py", line 62, in render
    result = block.nodelist.render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\defaulttags.py", line 209, in render
    nodelist.append(node.render_annotated(context))
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\defaulttags.py", line 309, in render
    return nodelist.render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\defaulttags.py", line 309, in render
    return nodelist.render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 936, in render
    bit = node.render_annotated(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 903, in render_annotated
    return self.render(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 986, in render
    output = self.filter_expression.resolve(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 670, in resolve
    obj = self.var.resolve(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 795, in resolve
    value = self._resolve_lookup(context)
  File "C:\Users\winkl\tj3\venv\lib\site-packages\django\template\base.py", line 857, in _resolve_lookup
    current = current()
  File "C:\Users\winkl\tj3\portfolios\models.py", line 321, in get_profit_loss_value_fees
    result = self.get_profit_loss_value() - self.get_fees()
TypeError: unsupported operand type(s) for -: 'NoneType' and 'float'

可能 self.type 您要么没有检查,要么检查但没有 returning 任何东西。如果您什么都不 return,python 将为您 return None

因此,如果 self.get_exit_cpu() 大于 0,但 self.type 是您未检查的内容,比如 "Long Long",那么 else 语句将不会' t 执行并且不会手动 returned - 意思是 None 将 returned.

你总是可以围绕 def get_profit_loss_value(self):

做一个包装
def get_profit_loss_value_or_None(self):
   # All code as now

def get_profit_loss_value(self):
    ret = self.get_profit_loss_value_or_None()
    return 0 if ret is None else ret

这也可以防止任何未来的更改导致无法预料的 None returns。