0

my model :

class Registration(models.Model):
    fio = models.CharField(verbose_name = 'fio', max_length = 100, null=True)
    phone = models.CharField(verbose_name ='phone', max_length = 16, null=True)
    date_register = models.DateTimeField(verbose_name = 'date registers', max_length = 100, null=True) 
    date_visit = models.DateField(verbose_name = 'date visit', blank=True, null=True)
    time_visit = models.CharField(verbose_name = 'time visit', null=True,max_length = 100)
    verification = models.BooleanField(default=False)

how to perform menegment-comand periodically, valid once an hour. Now I run the team manage.py del

import datetime
from django.core.management.base import BaseCommand
from django.utils import timezone
from registers.models import Registration

class Command(BaseCommand):
    help = 'delete unverified users'

    def handle(self, *args, **kwargs):

        regs = Registration.objects.filter(verification__in=["False"])
        for a in regs:
            if a.date_register <= (datetime.datetime.now() - datetime.timedelta(minutes=10)):
                rg = Registration.objects.get(date_register = a.date_register.strftime("%Y-%m-%d %H:%M:%S"))
                rg.delete()

How to make this command work even when the site starts every hour (p.s. after running the manage.py runserver command)

qwerty
  • 193
  • 4
  • 14
  • 2
    You can look into cron job. Set a cron job on your server to run the script every hour. https://crontab.guru/every-1-hour – Lian Jun 11 '20 at 07:28
  • @Lian tried to do as in the tutorial https://pypi.org/project/django-crontab/ . when I enter the comand `python manage.py crontab add` error ModuleNotFoundError: No module named 'fcntl' – qwerty Jun 11 '20 at 07:40
  • You have to make sure that your cron executes the command from the correct python environment. Whatever you do to run the task manually (activate python env, run command), same has to be fed to the crontab (or other cron mgmt service). – shad0w_wa1k3r Jun 11 '20 at 07:46
  • @shad0w_wa1k3r https://github.com/kraiz/django-crontab/issues/34#issuecomment-219454337 . I can't use it on windows? – qwerty Jun 11 '20 at 08:02
  • You can use anything that can schedule your runs, doesn't have to be crontab. See https://stackoverflow.com/questions/7195503/setting-up-a-cron-job-in-windows. – shad0w_wa1k3r Jun 11 '20 at 08:04
  • I have not used django-crontab but on Linux servers, crontab is available. I'm assuming your server is Linux. Otherwise find out how to schedule tasks on your server. For crontab, the config is just a file containing the schedule time, and command to run. You can edit the config file with `crontab -e` and add your command there, with full path to the python executable and the manage.py file. – Lian Jun 11 '20 at 09:19
  • @Lian I wanted to test this mechanism on my pc. With embedded server in django – qwerty Jun 11 '20 at 09:42
  • I would suggest if your server is Linux, test on the server. Not sure how you would do it on your computer (assuming Windows?) Maybe try WSL – Lian Jun 11 '20 at 09:47

0 Answers0