How do i calculate the due date from this model based on the date renewed. i want the due date to be 5 years after the date Renewed.
I'm pretty stuck. I need help.
This is my models.py file in Django
def get_deadline():
return dateRenewed() timedelta(days=1825)
class Trademark(models.Model):
trademarkName = models.CharField(max_length=100)
trademarkClass = models.CharField(max_length=100)
dateCreated = models.DateTimeField(default=timezone.now)
acknowledgeDoc = models.FileField(upload_to='acknowledge_docs', default='default.jpg')
acceptanceDoc = models.FileField(upload_to='acceptance_docs/', default='default.jpg')
cert = models.FileField(upload_to='trademark_cert/', default='default.jpg')
renewalDoc = models.FileField(upload_to='renewalDocs', default='default.jpg')
dateRenewed = models.DateTimeField(auto_now_add=False, auto_now=False, blank=True, null=True)
duedate = models.DateTimeField(default=get_deadline)
uploadedBy = models.ForeignKey(User, on_delete=models.RESTRICT)
```
CodePudding user response:
You can use timedelta
to add the years:
from django.utils import timezone
delta = timezone.timedelta(days=365*5) # create a timedelta object
duedate = dateRenewed delta
This doesn't handle leap years, though.
Accounting for leap years
The above code always counts 365 days for every year. But if you want to account for leap years, you'll need to add an extra day for every leap year:
import calendar
from django.utils import timezone
year_1 = dateRenewed.year
year_2 = year_1 5
leap_days = calendar.leapdays(year_1, year_2) # count leap days between two years
total_days = (365 * 5) leap_days
delta = timezone.timedelta(days=total_days)
duedate = dateRenewed delta
CodePudding user response:
I found a way to do it.
It's by using
@property
def Shelf life(self):
return self.dateRenewed datetime.timedelta(days=365*5)