[Answered ]-Get latest entries from two other tables per record


There is something called reverse relationship in django orm. You can use the lowercase name of the of the model in this case vehiclemileage and vehicleprice with the Vehicle model to get the result set.

This is how the query looks:

vehicles_with_latest_mileage_price = Vehicle.objects.values('vehiclemileage__odometer_reading','vehicleprice__price','vin','stock_number','year__year').order_by('-vehiclemileage__date_time_added','-vehicleprice__date_time_added')

This should return latest vehicles with its relevant price and mileage resulting in one query and returning one dataset.

sorry but you’ll have to scroll to see the entire query.


Leave a comment