DurationField() output formatting

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:
1

but what i want:
1

“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 ?

Use timesince Built-in template tags and filters | Django documentation | Django instead of your code.

In the template:

{{ begin|timesince:end }}

thx for the Input… but for the total of days from several periods i must be something like

{{ begin|forloop.counter0|timesince:end|forloop.last}}

… have not tried it yet but this will not work i think :wink:

sometimes i wonder why the most simple thinks take the longest time… :see_no_evil:
so the solution is VERY simple for this special case:

{{ total_duration_days|cut:" days, 0:00:00" }}