简体   繁体   中英

how to create periodictask in celery and django for an specific model instance

I have a Django model Person:

class Person(models.Model):
    first_name = models.CharField(max_length=100, blank=False, null=False)
    last_name = models.CharField(max_length=100, blank=False, null=False)
    cellphone = models.CharField(max_length=20)
    phone = models.CharField(max_length=15)
    email = models.EmailField()

and a model called Activity, which tracks throught a ForeignKey field the activities registered to a Person

I need to validate if a Person have not made any activity in 45 days so I can delete her from the database

I thought that a Celery PeriodicTasks could be a solution, but this tasks would have to run every day and query every person in the database to make the validation.

Is there a way to create an specific scheduled tasks for every Person instance that runs in 45 days after the instance is saved in the database for the first time?

You can try the following:

1) Add a field 'last_activity' to your model which automatically updates the timestamp of the last activity day of the user.

2) Now create a task which will run after 45 days of the first instance. It will check for all the users whether the timestamp of last_activity_day is 45 days older or not. If it is, then it will delete the user.

But its only the first 45 days you can save the processing for. After that you need to run the task everyday ie, on 46th day, 47th day, 48th day and so on to detect those users whose last_activity was on day 2, day 3 and so on.

Hence its better to run a daily task as you yourself mentioned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM