我有一个带有时间字段的模型,我需要在所有实例中对时间字段求和。
这是我尝试过的:
interface State {
name: string;
}
interface Props {
}
export class Profile extends React.Component<Props, State> {
public state: State;
public props: Props;
constructor(props){
super(props);
this.state = {
name: 'baz111'
};
}
public render() {
return (
<section>
<section>
<h3>profile 1</h3>
<div>baz</div>
</section>
<section>
<h3>profile 2</h3>
<div>{this.state.name}</div>
</section>
</section>
)
}
}
我收到此错误:
total_hours = Day.objects.filter(date__year=current_year, date__month=current_month).aggregate(Sum('total_work_hours'))
print(total_hours)
有解决方法吗?我现在想坚持使用SQLite。
答案 0 :(得分:0)
在不更改数据库的情况下,您必须在Python中执行此操作。您可以使用values_list
每天拉出total_work_hours
并将它们相加,从而以相当便宜的方式执行此操作。
hours = Day.objects.filter(
date__year=current_year,
date__month=current_month
).values_list('total_work_hours', flat=True)
total_hours = sum(hours)
print(total_hours)
答案 1 :(得分:0)
好的,这就是我最终做的事情。我添加了一个地图,将时间对象转换为小时。
times = Day.objects.filter(date__year=current_year, date__month=current_month).values_list('total_work_hours', flat=True)
hours = map(lambda time: time.hour + (time.minute / 60.0), times)
total_hours = sum(hours)