0

I have a field in my model

Step=models.IntegerField()

I call it normally in the template with the following:

{{user.step}}

Now I want this field to automatically increase after every year

What is the best approach here?

Nico Griffioen
  • 5,143
  • 2
  • 27
  • 36

1 Answers1

0

Is it based on some timestamp? like a birth date?

In this case you have few option:

  1. Make it a property and recalculate it before sending data to template

    from dateutil.relativedelta import relativedelta
    
    @property
    def step(self):
        return relativedelta(datetime.now(), self.timestamp).years
    
  2. Make cronjob that will update step field every day using method above

Bartosz Stasiak
  • 1,415
  • 1
  • 4
  • 9
  • So I have to install cronjob library right? I was thinking of a way to do it without any third party library – Raji Muhammad May 25 '23 at 19:42
  • 1
    Cron is build into Linux. You should be able to run django command with something like "* * * * * python /path/to/project/manage.py management_command" – Bartosz Stasiak May 26 '23 at 09:29