【问题标题】:How to add two def function in python如何在python中添加两个def函数
【发布时间】:2019-09-19 21:45:49
【问题描述】:
def get_total_co2_electric(self, obj):
        totalpieces = ElectricBike.objects.all().aggregate(total_co2_electric=Sum('co2'))
        return totalpieces["total_co2_electric"]

# Total Co2 save by Electric
def get_total_co2_classic(self, obj):
    totalprice = ClassicBike.objects.all().aggregate(total_co2_classic=Sum('co2'))
    return totalprice["total_co2_classic"]

def get_total_co2(self):
    totalprice = (self.get_total_co2_classic() + self.get_total_co2_electric())
    return totalprice

我正在尝试添加这两个 def 标记并获得结果,但出现错误“需要 1 个位置参数,但给出了 2 个” 那么如何添加这个

【问题讨论】:

  • 去掉obj参数。
  • 我做了兄弟,但我仍然遇到同样的错误
  • 能否提供完整的错误信息?从您的问题中不清楚给出了 2 个论点。
  • @RobStreeting "get_total_co2_electric() 接受 1 个位置参数,但给出了 2 个" 这是错误消息,我从所有对象中删除了 obj
  • 你是否将这些函数添加到一个类中?

标签: python-3.x


【解决方案1】:

你删除 obj 并做一些类似的事情

def get_total_co2_electric(self):
    totalpieces = ElectricBike.objects.all().aggregate(total_co2_electric=Sum('co2'))
    return totalpieces["total_co2_electric"]

【讨论】:

  • 抱歉
  • from . import views urlpatterns = [ path('dashboard',views.Dashboard.as_view()) ] 这是 urls.py
  • views.Dashboard.as_view() 用于基于类的视图,但您使用的是 Def,删除 as_view() 并变为 from 。导入视图 urlpatterns = [ path('dashboard',views.Dashboard, name='Dashboard') ]
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-27
  • 1970-01-01
  • 1970-01-01
  • 2020-07-18
  • 2013-05-16
  • 1970-01-01
相关资源
最近更新 更多