うまくいけば、私が達成しようとしていることを説明できます。結果を達成するのに問題はありませんが、これがおそらく最善の方法ではないことはわかっています。
日付ごとにいくつかのエントリを含むテーブルがあります。現在の月からこれらのエントリを取得し、それらを週ごとにリストに配置してから、曜日ごとにテーブルの値を合計しようとしています。最終結果は次のようになります。
{44: {4: Decimal('2.80'), 5: Decimal('6.30')}, 45: {1: Decimal('8.90'), 2: Decimal('10.60')}}
解決策があります。しかし、これが最善の方法ではないことはわかっています。これを改善する方法についてのアイデアはありますか?
#Queries the database and returns time objects that have fields 'hours' and 'date'
time = self.month_time()
r = {}
for t in time:
#Get the week of the year number
week = t.date.isocalendar()[1]
#Get the day of the week number
day = t.date.isoweekday()
if week not in r:
r.update({week:{}})
if day not in r[week]:
r[week][day] = 0
r[week][day] = r[week][day] + t.hours