1๐
Just simply use F class to compare the time you wanted with one of the fields (start_working_date) of the model.
two tips:
1-for now time its better to use django timezone
2-you should use .date() method of now to be able to compare it with your start_working_date
from django.db.models import F
from django.utils import timezone
employees = Employee.objects.annotate(
duration=timezone.now().date() - F('start_working_date')
)
0๐
Just try this __range
date_range = [from_date, to_date ]
emp = Employee.objects.filter(start_working_date__range=date_range)
๐คAmar
0๐
Use Value
to pass and use a value in a query expression
from datetime import date
from django.db.models import F, Value
employees = Employee.objects.annotate(
duration=Value(date.today()) - F('start_working_date')
)
๐คIain Shelvington
- [Answered ]-Test django post with factory_boy
- [Answered ]-Django Rest โ Using a SerializerMethodField and getting an error: 'QuerySet' object has no attribute 'birth_date'
Source:stackexchange.com