Irfan Harun
Irfan Harun

Reputation: 1059

Custom model manager that uses model methods in Django

in my model, i have 3 methods. i want to use get_days_remaining method to get all policies expiring in 30 days.

Class TravelPolicy(models.Model):
    ....
    ....

def get_period_of_cover(self):
    some logic
def get_days_left_to_run(self):
    some logic

def get_days_remaining(self):
    return float(self.get_period_of_cover()) - float(self.get_days_left_to_run())

currently doing TravelPolicy.objects.all() returns over 10,000 policies, while i might policies in order of few 100s expiring in next 30 days. How can i get only the policies expiring in 30 days using get_days_remaining method within my views before passing it to html file ?

Upvotes: 0

Views: 38

Answers (1)

HerculesOfHell
HerculesOfHell

Reputation: 117

You may try something like the following in your view (using iterator will not load all the policies in memory):

my_policies = [i for i in TravelPolicy.objects.iterator() if i.get_days_remaining() < 30]

Upvotes: 2

Related Questions