首页 > 解决方案 > Django TypeError:未提供异常消息

问题描述

我在理解此错误消息时遇到问题..

我在视图中写了这行代码

    qs = Stockdata.objects.filter(User=self.request.user, Company=company_details.pk, Date__gte=selectdatefield_details.Start_Date, Date__lte=selectdatefield_details.End_Date)
    total = qs.annotate(Sum('salestock__Quantity'))['the_sum']
    total2 = qs.annotate(Sum('purchasestock__Quantity_p'))['the_sum']
    tqty = total2 - total 
    context['Totalquantity'] = tqty
    context['Totalquantitysales'] = total
    context['Totalquantitypurchase'] = total2

有人知道我在代码中做错了什么吗?

谢谢

追溯:

Traceback:

  File "C:\Users\HP\myEnv\lib\site-packages\django\core\handlers\exception.py" in inner
 35.             response = get_response(request)

 File "C:\Users\HP\myEnv\lib\site-packages\django\core\handlers\base.py" in _get_response
  128.                 response = self.process_exception_by_middleware(e, request)

 File "C:\Users\HP\myEnv\lib\site-packages\django\core\handlers\base.py" in _get_response
 126.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

 File "C:\Users\HP\myEnv\lib\site-packages\django\views\generic\base.py" in view
 69.             return self.dispatch(request, *args, **kwargs)

 File "C:\Users\HP\myEnv\lib\site-packages\django\contrib\auth\mixins.py" in dispatch
 52.         return super().dispatch(request, *args, **kwargs)

File "C:\Users\HP\myEnv\lib\site-packages\django\views\generic\base.py" in dispatch
89.         return handler(request, *args, **kwargs)

File "C:\Users\HP\myEnv\lib\site-packages\django\views\generic\list.py" in get
157.         context = self.get_context_data()

File "C:\Users\HP\My Documents\github\erpcloud\erpcloud\stockkeeping\views.py" in get_context_data
446.        total = qs.annotate(Sum('salestock__Quantity'))['the_sum']

File "C:\Users\HP\myEnv\lib\site-packages\django\db\models\query.py" in __getitem__
282.             raise TypeError

Exception Type: TypeError at /stockkeeping/company/31/date/13/
Exception Value: 

标签: djangodjango-views

解决方案


错误是您无法从Queryset对象中按键获取值

阅读更多关于Django 中的注解


推荐阅读