代码之家  ›  专栏  ›  技术社区  ›  Niladry Kar

Django TypeError:未提供异常消息

  •  0
  • Niladry Kar  · 技术社区  · 7 年前

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

    我已经在视图中编写了这行代码

        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: 
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   py_dude    7 年前

    错误是您无法通过键从中获取值 Queryset

    阅读更多关于 annotations in Django