2017-06-09 2 views
0

Ich versuche, Anzahl der Berechnungen pro Monat in den letzten 12 Monaten in Django zu bekommen. Ich habe Funktion wie folgt:Django Lambda-Funktion gibt falsche Ergebnisse

def calculations_per_month_last_12_months(member_number): 
    items = list(Calculations.objects 
       .filter(user__member__number=member_number) 
       .filter(price_date__gte=datetime.datetime.now().today() - relativedelta(months=12)) 
       .annotate(month=TruncMonth('price_date')) 
       .values('month') 
       .annotate(total=Count('id')) 
       .values('month', 'total') 
       .order_by('month')) 

    result = [] 
    for month in range(12): 
     date = timezone.now() - relativedelta(months=month) 

     month_results = list(filter(lambda i: date <= i['month'] < (date + relativedelta(months=1)), items)) 

     month_result = 0 
     if month_results: 
      month_result = month_results[0]['total'] 

     result.append({ 
      'month': date.strftime('%B'), 
      'total_calculations': month_result 
     }) 

    return result 

In Artikel Ich habe, was ich will, aber das Problem ist in for Schleife. Zum Beispiel bekomme ich für kann was für Juni sein muss.

Das Ergebnis, das ich für einen Benutzer zu bekommen ist:

[{ 
    'month': 'June', 
    'total_calculations': 0 
}, { 
    'month': 'May', 
    'total_calculations': 1 
}, { 
    'month': 'April', 
    'total_calculations': 4 
}, { 
    'month': 'March', 
    'total_calculations': 0 
}, { 
    'month': 'February', 
    'total_calculations': 0 
}, { 
    'month': 'January', 
    'total_calculations': 0 
}, { 
    'month': 'December', 
    'total_calculations': 0 
}, { 
    'month': 'November', 
    'total_calculations': 0 
}, { 
    'month': 'October', 
    'total_calculations': 0 
}, { 
    'month': 'September', 
    'total_calculations': 0 
}, { 
    'month': 'August', 
    'total_calculations': 0 
}, { 
    'month': 'July', 
    'total_calculations': 0 
}] 

So total_calculations für Mai total_calculations für Juni und so weiter sein sollte ..

Die price_date in der Datenbank für die Benutzer:

enter image description here

So gibt es alles in Ordnung. 1 Berechnung im Juni und 4 im Mai.

Irgendeine Idee was mache ich falsch?

EDIT

Artikel:

[{ 
    'month': datetime.datetime(2017, 5, 1, 0, 0, tzinfo = < django.utils.timezone.LocalTimezone object at 0x10774fb00 >), 
    'total': 4 
}, { 
    'month': datetime.datetime(2017, 6, 1, 0, 0, tzinfo = < django.utils.timezone.LocalTimezone object at 0x10774fb00 >), 
    'total': 1 
}] 
+0

Diese Art von Problem kann nur durch Debuggen sortiert werden. Zum Beispiel, um herauszufinden, was Ihr Lambda tut, müssen wir wissen, was Ihre "Artikel" enthält, aber wie? – e4c5

+0

@ e4c5 Ich habe meine Frage aktualisiert. – Boky

+0

danke dafür. Selbst dann braucht es viel Kopfrechnen, damit jemand das herausfinden kann. Kann ich Ihnen vorschlagen, einen interaktiven Debugger zu installieren? – e4c5

Antwort

0

ich es 1 Monat i['date'] + relativedelta(months=1) Zugabe gelöst, das ist wahrscheinlich nicht ideal, aber es funktioniert.

Statt also:

month_results = list(filter(lambda i: date <= i['date'] < (date + relativedelta(months=1)), items)) 

jetzt habe ich:

month_results = list(filter(lambda i: date <= i['date'] + relativedelta(months=1) < (date + relativedelta(months=1)), items)) 
Verwandte Themen