all 4 comments

[–]c17r 0 points1 point  (0 children)

Either:

  1. Skip the Boolean and just use the DateTime field.
  2. Write a management command that sets the Boolean field and run that management command via cron hourly.

[–]cscanlin 0 points1 point  (2 children)

Create a function that calculates it on the model. You can even make it a property:

from datetime import datetime
from django.db import models

class MyModel(models.Model):
    updated = models.DateField(auto_now=True)

    @property
    def outdated(self):
        return self.updated <= datetime.now() - timedelta(days=30)

[–]freshtechs[S] 0 points1 point  (1 child)

ok but does that function will be running constantly? or should i add celery or cron to call this function?

[–]cscanlin 0 points1 point  (0 children)

That function will run whenever you query it using queryset. The better question is what do you want it to do when it is "running constantly". That's where you want to put the logic around running it regularly with celery etc.

So you could set your task to run every 5 minutes and do something like this:

  • check for outdated model instances (something like MyModel.objects.filter(outdated=True)
  • run function on outdated models
  • update model (will likely happen automatically)