R
R
Radiss2020-05-07 14:10:18
Django
Radiss, 2020-05-07 14:10:18

How to write an implementation of the handle method for a command that displays tasks that were completed in the last days of days?

An example of calling such a command:

(my-venv) $ python manage.py tasks_report_completed --days 5


You want this query to display all tasks completed in the last five
days. If the days parameter is not specified, then take 3 days.

Here is an example view of tasks/management/commands/tasks_report_completed.py

# coding: utf-8
from django.core.management import BaseCommand
from datetime import datetime
from tasks.models import TodoItem
class Command(BaseCommand):
help = u"Displays all tasks completed in the last `days` days (default=3
days)"
def add_arguments(self, parser):
parser.add_argument('--days', dest='days', type=int, default=3)
def handle(self, *args, **options):
now = datetime.now(timezone.utc)
# ...


It doesn't work like this

def handle(self, *args, **options):
        now = datetime.now(timezone.utc)
        for t in TodoItem.objects.filter(is_completed=False):
            if (now - t.created).days >= options['--_days']:
                print("Задача:", t, t.created)

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question