TypeError:不支持的操作数类型

时间:2012-09-12 01:59:58

标签: python django

我在尝试检查运输变量是否为空时遇到类型错误(见下文)。该字段是使用django设置的货币字段...有什么正确的方法来检查它是否为空。

    Traceback (most recent call last):

    File "core/handlers/base.py", line 111, in get_response
    response = callback(request, *callback_args, **callback_kwargs)

File "contrib/admin/options.py", line 307, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)

File "utils/decorators.py", line 93, in _wrapped_view
response = view_func(request, *args, **kwargs)

File "views/decorators/cache.py", line 79, in _wrapped_view_func
response = view_func(request, *args, **kwargs)

File "contrib/admin/sites.py", line 197, in inner
return view(request, *args, **kwargs)

File "admin.py", line 21, in changelist_view
return super(HeldOrderAdmin,self).changelist_view(request, extra_context=extra_context)

File "utils/decorators.py", line 28, in _wrapper
return bound_func(*args, **kwargs)

File "utils/decorators.py", line 93, in _wrapped_view
response = view_func(request, *args, **kwargs)

File "utils/decorators.py", line 24, in bound_func
return func(self, *args2, **kwargs2)

File "contrib/admin/options.py", line 1179, in changelist_view
], context, context_instance=context_instance)

File "shortcuts/__init__.py", line 20, in render_to_response
return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)

File "template/loader.py", line 188, in render_to_string
return t.render(context_instance)

File "template/base.py", line 123, in render
return self._render(context)

File "template/base.py", line 117, in _render
return self.nodelist.render(context)

File "template/base.py", line 744, in render
bits.append(self.render_node(node, context))

File "template/base.py", line 757, in render_node
return node.render(context)

File "template/loader_tags.py", line 127, in render
return compiled_parent._render(context)

File "template/base.py", line 117, in _render
return self.nodelist.render(context)

File "template/base.py", line 744, in render
bits.append(self.render_node(node, context))

File "template/base.py", line 757, in render_node
return node.render(context)

File "template/loader_tags.py", line 127, in render
return compiled_parent._render(context)

File "template/base.py", line 117, in _render
return self.nodelist.render(context)

File "template/base.py", line 744, in render
bits.append(self.render_node(node, context))

File "template/base.py", line 757, in render_node
return node.render(context)

File "template/loader_tags.py", line 127, in render
return compiled_parent._render(context)

File "template/base.py", line 117, in _render
return self.nodelist.render(context)

File "template/base.py", line 744, in render
bits.append(self.render_node(node, context))

File "template/base.py", line 757, in render_node
return node.render(context)

File "template/loader_tags.py", line 64, in render
result = block.nodelist.render(context)

File "template/base.py", line 744, in render
bits.append(self.render_node(node, context))

File "template/base.py", line 757, in render_node
return node.render(context)

File "template/loader_tags.py", line 64, in render
result = block.nodelist.render(context)

File "template/base.py", line 744, in render
bits.append(self.render_node(node, context))

File "template/base.py", line 757, in render_node
return node.render(context)

File "template/base.py", line 921, in render
dict = func(*args)

File "contrib/admin/templatetags/admin_list.py", line 232, in result_list
'results': list(results(cl))}

File "contrib/admin/templatetags/admin_list.py", line 217, in results
yield ResultList(None, items_for_result(cl, res, None))

File "contrib/admin/templatetags/admin_list.py", line 209, in __init__
super(ResultList, self).__init__(*items)

File "contrib/admin/templatetags/admin_list.py", line 137, in items_for_result
f, attr, value = lookup_field(field_name, result, cl.model_admin)

File "contrib/admin/util.py", line 214, in lookup_field
value = attr(obj)

File "admin.py", line 50, in total
return '$%s' % (obj.order.total(),)

File "models.py", line 234, in total
if item.total is not None:

File "models.py", line 356, in total
shipping = 0

TypeError: unsupported operand type(s) for +: 'NoneType' and 'Decimal'

1 个答案:

答案 0 :(得分:0)

total += item.total这段代码可能是错误的。检查totalitem.totalNoneType

因为您的错误是TypeError: unsupported operand type(s) for +: 'NoneType' and 'Decimal',所以它由运营商+

引起