Hi there, it’s me again…
working on my first Django-Project since few weeks and actually wondering how to format the Output-Format of a DurationField in a template.
What i have:
in models.py:
class Missions(models.Model):
...
begin = models.DateField(null=True, blank=True)
end = models.DateField(null=True, blank=True)
employee = models.ForeignKey(Employees, on_delete=models.CASCADE, null=True)
objects = Duration_Manager()
in managers.py
from django.db import models
from django.db.models import F, fields, ExpressionWrapper, Sum
from employees.fields import CustomDurationField
class Duration_Manager(models.Manager):
def with_duration_days(self):
return self.annotate(
duration_days= ExpressionWrapper(
(F('end') - F('begin')), output_field=CustomDurationField()
)
)
def total_duration_days(self, employee):
return self.with_duration_days().filter(employee=employee).aggregate(
total_duration=Sum('duration_days')
)['total_duration']
in views.py
context = {
...
'missions': Missions.objects.filter(employee_id=request.POST['id']).order_by('-begin'),
'examinations': Examinations.objects.filter(employee_id=request.POST['id']).order_by('-date'),
'total_duration_days': Foreign_Missions.objects.total_duration_days(employee),
}
return render(request, 'show.html', context)
and in tmeplate:
<div class="...."><b>Missions</b> [Tage: {{ total_duration_days }}]</div>
what i get:
but what i want:
“Tage” means “days” in german, so an option would be render output in German an remove only timestamp…but i think effort for this (if possible) is in the end the same ?