Я попытался структурировать свой код так, чтобы, если 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:
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'