0

I have a gallery model in my models.py. I want to automatically move the data to another table called as Archive if created date is more than 30 days.

This is my models.py

class Gallery(models.Model):
    id= models.AutoField(primary_key=True)
    name = models.CharField(max_length=100)
    image = models.ImageField(upload_to='images/')
    video = models.FileField(upload_to='videos/', validators=[FileExtensionValidator( ['mp4', 'mov', 'mkv'] )])
    tag = models.CharField(max_length=100)
    classes = models.ForeignKey(Classes, null=True, blank=True, on_delete=models.CASCADE)
    level = models.ForeignKey(Level, null=True, blank=True, on_delete=models.CASCADE)
    uploded_date = models.DateTimeField(auto_now_add=True)
    

class Archive(models.Model):
    id= models.AutoField(primary_key=True)
    name = models.CharField(max_length=100)
    image = models.ImageField(upload_to='images/')
    video = models.FileField(upload_to='videos/', validators=[FileExtensionValidator( ['mp4', 'mov', 'mkv'] )])
    tag = models.CharField(max_length=100)
    classes = models.ForeignKey(Classes, null=True, blank=True, on_delete=models.CASCADE)
    level = models.ForeignKey(Level, null=True, blank=True, on_delete=models.CASCADE)
    uploded_date = models.DateTimeField(auto_now_add=True)
    

I have thought of creating signals and sending signals to Archive like this. I have already defined the signal.

def date_is_old(self):
        if self.uploded_date > (datetime.datetime.now()-timedelta(days=15)):
            date_is_old.send(self.__class__)

But this method seems to be ineffective as it only sends signal at time of creation of new record. I need the function to always check and If creation is past 30 days automatically move data to Archive. Is there some way to handle this? I am really out of option tried modifying create method and save methods on table "Gallery".

Sahil Mohile
  • 99
  • 1
  • 9
  • 1
    i never used but you can use `celery` to schedule task as per your need. once a day once a hour whatever you want – Hemal Patel Nov 21 '22 at 12:45
  • `celery` is nice option but it takes some load on server. So I was thinking if there is any way in django or sql. – Sahil Mohile Nov 21 '22 at 13:18
  • 1
    It depends on you server OS and DBMS, but maybe you can use a strategy like one of these ones https://blog.sqlbackupandftp.com/postgresql-job-scheduler (if may differ if you are using another DBMS or OS) – Emanuele Scarabattoli Nov 21 '22 at 13:53
  • @EmanueleScarabattoli Okay. I will try with `Celery`. Thank you for suggestions. – Sahil Mohile Nov 21 '22 at 13:56
  • Cron job if you have control of the host system and want to be fancy: https://stackoverflow.com/questions/16717930/how-to-run-crontab-job-every-week-on-sunday. You could for example have a daily cron job calling a custom django command – Clepsyd Nov 21 '22 at 23:36
  • @Clepsyd I can do but as the app runs inside docker it sorta becomes a hassle. Celery alternative sounds better to me – Sahil Mohile Nov 22 '22 at 09:08

0 Answers0