1👍
You can do this in one query using double underscore notation and F
expressions:
from django.db.models import F
route_points = RoutePoint.objects.filter(
vehicle__travel__begin__lt=F("datetime"),
vehicle__travel__end__gt=F("datetime")
)
then simply iterate over the route_points
queryset:
for route_point in route_points:
print(route_point.vehicle, route_point.point, route_point.datetime)
0👍
You can filter the Travel Queryset:
start_date = '2022-12-30'
end_date = '2023-01-06'
travel_qs = Travel.objects.filter(begin__gt=start_date,
end__lt=end_date).values('vehicle_id', 'begin', 'end')
Now you can use travel_qs
to retrieve the Points from RoutePoint:
route_point_qs = RoutePoint.objects.filter(vehicle_id__in=travel_qs.values('vehicle_id'),
datetime__gt=travel_qs.values('begin'),
datetime__lt=travel_qs.values('end')).values('vehicle_id', 'point', 'datetime')
- [Answered ]-Python for loop not return multiple dict
- [Answered ]-Django How can we generate app wise log with app name directory and todaydate.log file
- [Answered ]-Django query database and send data to template
- [Answered ]-Django custom authentication doesn't redirect as expected
0👍
I did it like this:
vehicles_with_dates = travels.values('vehicle_id').annotate(min_dt=Min('begin'), max_dt=Max('end'))
route_points = RoutePoint.objects.filter(
vehicle_id__in=vehicles_with_dates.values('vehicle_id'),
datetime__gt=vehicles_with_dates.values('min_dt'),
datetime__lt=vehicles_with_dates.values('max_dt')
)
It worked perfectly fine, thanks to Iqbal Hussain.
- [Answered ]-How to register a new Model in Django-Oscar Dashboard?
- [Answered ]-How to hide / display a forms with a combo box
Source:stackexchange.com