class CompanyDetail(TemplateVariables, LoggedInMixin, DetailView):
""" """
model = Company
template_name = 'company/company_detail.html'
def cars(self):
return Car.objects.filter(Company_id=context['company'].id)
def get_context_data(self, **kwargs):
""" filtering cars assigned to company with last coord"""
context = super(CompanyDetail, self).get_context_data(**kwargs)
CarList = Car.objects.filter(Company_id=context['company'].id)
PointList = []
CarListFiltered = []
for car in CarList:
if Points.objects.filter(Car_id =
car.id).exists():
point = Points.objects.filter(Car_id =
car.id).order_by('CreatedTime').last()
PointList.append( point)
CarListFiltered.append(car)
context['Cars'] = CarListFiltered
context['PointList'] = zip(CarListFiltered, PointList)
return context
This view returns some additional data but main idea is clear (line with Car.object.filter)