Updated Django model days counting – month start error

On February 1st I got an error on my Work Accounting system. My beautiful graphs are no longer displayed. The fact is that earlier I used a while loop to list the working days – I decreased the DAY value by 1. This is not applicable outside the month.

To solve this problem, I used the timedelta class. Everything is working great now. I also applied the changes to the calculation of the average working time per day for the last week.

try:
    username = request.user.id
    field_name = 'date_added'
    obj = Addinfo.objects.filter(work_user=username).first()
    field_object = Addinfo._meta.get_field(field_name)
    first_date_full = field_object.value_from_object(obj)
    first_date = first_date_full.date()
    delta = datetime.now().date() - first_date
    n = delta.days

    x = []
    y = []
    i = 0
    while i<=n:
        daywork = datetime.now().date() -timedelta(days=i)
        dayview = Addinfo.objects.filter(date_added__date=daywork).filter(work_user=username).values_list('work_minutes', flat=True)
        y.append(sum([int(minn) for minn in dayview]))
        x.append(i)
        i += 1
except:
    x = []
    y = []

chart = get_plot_svg(x, y)

Code for avg time counting:

weeksum = []
i = 0
while i<=6:
    daywork = datetime.now().date() -timedelta(days=i)
    dayview = Addinfo.objects.filter(date_added__date=daywork).filter(work_user=username).values_list('work_minutes', flat=True)
    weeksum.append(sum([int(minn) for minn in dayview]))
    i += 1
avgmins = sum([int(minnweek) for minnweek in weeksum]) // 7

 

See also  How to get current month values from Django Model, convert to int() and sum?
Author: admin

Leave a Reply

Your email address will not be published. Required fields are marked *