4๐
I would make a method on the Employee object.
class Employee(models.Model):
...
def ends_within_50_days(self):
return (date.today() - self.end_date).days <= 50
Now you can just do:
{% if instance.ends_within_50_days %}
๐คDaniel Roseman
3๐
As suggested in the previous answer, you could make a method, or Iโd go with property, in your model class.
Assuming that you have something like this:
from datetime import date
from django.db import models
class Employee(models.Model):
end_date = models.DateField()
# ... rest ...
@property
def age_in_days(self):
today = date.today()
result = self.end_date - today
return result.days
In your template you can check if it is less than 50 days old:
{% if instance.age_in_days <= 50 %}
The property age_in_days
will return the difference in days between today and the value of end_date
as an integer. This should give you more flexibility in case you want to check not only if it is less than 50 days old. Eventually that requirement might change. Then you could reuse the property without refactoring your models.
You can still define a method in your model class to check if the instance ended within the last 50 days:
def ended_in_the_last_50_days(self):
return self.age_in_days <= 50
๐คcezar
- [Django]-How to restrict access for staff users to see only their information in Django admin page?
- [Django]-Django โ overiding get_fieldset in ModelAdmin returns a field twice
Source:stackexchange.com