Run one time for Django model calculated property

class Company_Car(models.Model):
    
    @property
    def days_left(self):
        print("RUNNED PROPERTY")
        if self.date_valid is not None and self.date_valid >= datetime.datetime.now().date():
            return (self.date_valid - datetime.datetime.now().date()).days
        
    added            = models.DateTimeField(auto_now_add=True)
    status           = models.BooleanField(default=True)
    company          = models.ForeignKey(Company, on_delete=models.DO_NOTHING)
    date_valid       = models.DateField(null=True, blank=True)

Each time when i ask this property for same record it executes and it's not good

My goal - property should run only once and return calculated data

How it can done?

Found a solution: there is a build-in decorator @cached_rpoperty

from django.utils.functional import cached_property ## HERE


class Company_Car(models.Model):
    
    @cached_property ###  AND HERE
    def days_left(self):
        print("RUNNED PROPERTY")
        if self.date_valid is not None and self.date_valid >= datetime.datetime.now().date():
            return (self.date_valid - datetime.datetime.now().date()).days
        
    added            = models.DateTimeField(auto_now_add=True)
    status           = models.BooleanField(default=True)
    company          = models.ForeignKey(Company, on_delete=models.DO_NOTHING)
    date_valid       = models.DateField(null=True, blank=True)
Back to Top