【问题标题】:Django TypeError: No exception message suppliedDjango TypeError:未提供异常消息
【发布时间】:2018-12-05 07:34:23
【问题描述】:

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

这行代码我已经写在我的视图里了

    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: 

【问题讨论】:

  • 添加错误回溯
  • 我已经编辑了我的代码...

标签: django django-views


【解决方案1】:

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

阅读更多关于annotations in Django

【讨论】:

  • 有什么解决办法吗?
  • 是的,使用aggregate函数
  • 是的,我可以这样做,但它不会给我 Stockdata 的个人总数...它将给我所有 stockdata 的总数...
  • @NiladryKar 这行得通吗? qs.annotate(the_sum=Sum('salestock__Quantity')).values('the_sum')
猜你喜欢
  • 1970-01-01
  • 2015-01-04
  • 1970-01-01
  • 1970-01-01
  • 2019-05-17
  • 2014-02-17
  • 1970-01-01
  • 2018-08-23
  • 2020-11-29
相关资源
最近更新 更多