'Finding out the difference of months to int in django
Im trying to get the difference of months in a project from it's start date to it's end date, and compute it to the monthly updates to show the percentage of a project progress, but my current code doesn't give value to compute. If I remove the .month it says unsupported operand type(s) for /: 'int' and 'datetime.timedelta'
Models.py
class Projects(models.Model):
id=models.AutoField(primary_key=True)
project_name=models.CharField(max_length=255)
project_manager=models.ForeignKey(CustomUser,on_delete=models.CASCADE, limit_choices_to={'is_project_manager' : True})
client_name=models.ForeignKey(Clients,on_delete=models.CASCADE, null=True)
project_pic=models.ImageField(upload_to='static/website/project-images')
project_start_date=models.DateField(null=True)
project_end_date=models.DateField(null=True)
project_description=models.TextField(null=True)
created_at=models.DateTimeField(auto_now_add=True)
updated_at=models.DateTimeField(auto_now=True)
is_draft = models.BooleanField(default=True)
objects=models.Manager()
class Meta:
verbose_name_plural = 'Project'
def __str__(self):
return f'{self.project_name}'
class MonthlyProjectStatus(models.Model):
id=models.AutoField(primary_key=True)
project_name=models.ForeignKey(Projects,on_delete=models.CASCADE)
project_manager=models.ForeignKey(CustomUser,on_delete=models.CASCADE, null=True)
client_name=models.ForeignKey(Clients,on_delete=models.CASCADE, null=True)
attachments=models.FileField(upload_to='static/website/files', null=True)
project_update=models.TextField(null=True)
created_at=models.DateTimeField(auto_now_add=True)
updated_at=models.DateTimeField(auto_now=True)
is_draft = models.BooleanField(default=True)
class Meta:
verbose_name_plural = 'Monthly Status'
def __str__(self):
return f'{self.project_name}'
Views.py
def project_details(request, pk):
projects = Projects.objects.get(id=pk)
employed = Employee.objects.filter(project_site=projects)
invents = Project_Inventory.objects.filter(project_site=projects)
months = MonthlyProjectStatus.objects.filter(project_name=projects).count()
dt = projects.project_start_date.month - projects.project_end_date.month
sums = (months)/(dt) * 100
context = {
'projects' : projects,
'employed' : employed,
'invents' : invents,
'dt' : dt,
'months' : months,
'sums' : sums,
}
template_name ='project-admin/project-details.html'
return render(request, template_name, context)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
