from django.views.generic import DetailView from .models import Designer from django.db.models import Count class DesignerDetailView(DetailView): model = Designer template_name = 'designers/designer_detail.html' context_object_name = 'designer' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) # Get all rides by this designer context['rides'] = self.object.rides.select_related( 'park', 'manufacturer', 'coaster_stats' ).order_by('-opening_date') # Get stats context['stats'] = { 'total_rides': self.object.rides.count(), 'total_parks': self.object.rides.values('park').distinct().count(), 'total_coasters': self.object.rides.filter(category='RC').count(), 'total_countries': self.object.rides.values( 'park__location__country' ).distinct().count(), } return context