I’m making a todo list in django. I have a model with some properties:
class Action(models.Model):
name = models.CharField("Action Name", max_length=200, unique = True)
slug = models.SlugField(max_length=100, blank=True)
complete = models.BooleanField(default=False, verbose_name="Complete?")
effort = models.IntegerField("Effort Level", choices = LEVELS, default = 3)
importance = models.IntegerField("Importance", choices = LEVELS, default = 3)
enjoyment = models.IntegerField("Enjoyment", choices = LEVELS, default = 3)
days_to_expiration = models.IntegerField("Days to Expiration", choices = DAYS_TO_EXPIRATION, default = 7)
reset_date = models.DateField("Date of Action Reset", blank=True, null=True)
I want to sort based on priority. I define priority as:
priority = (1 + (today's date - reset_date) / days_to_expiration) * importance
This just scales up importance based on how “overdue” the task is.
Now when I pull a list of incomplete actions, I can calculate this priority number for each action item, but that doesn’t seem efficient since I will end up calculating the same numbers multiple times. I could define a new field in the model:
priority = models.DecimalField("Priority", max_digits=4, decimal_places=2, blank = True, null = True)
and schedule a function to run once a day to calculate the new priority per task.
But I want to know if it’s possible to program the priority calculation directly into the model. So the priority field in the Action model is necessarily by design a function of importance and days_to_expiration. It would be updated dynamically using the values of importance, days_to_expiration, and today’s date. That way, priority would always be calculated automatically when I need it. Is this a possibility?
You could do something like:
Then, you can access it like any other attribute on your model