From 009ff5ae9685270ed72100ed7088d2a39bf0221f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Tue, 11 Jan 2022 20:45:01 +0100 Subject: [PATCH] Improve a few queries --- shimatta_kenkyusho/parts/views.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/shimatta_kenkyusho/parts/views.py b/shimatta_kenkyusho/parts/views.py index bc7ab0d..3da80e8 100644 --- a/shimatta_kenkyusho/parts/views.py +++ b/shimatta_kenkyusho/parts/views.py @@ -162,7 +162,7 @@ class ComponentView(LoginRequiredMixin, BaseTemplateMixin, TemplateView): return queryset def get_component_queryset_from_advanced_search(self, cleaned_data): - queryset = Component.objects.select_related('manufacturer', 'package').all() + queryset = Component.objects.select_related('manufacturer', 'package', 'component_type').prefetch_related('componentparameter_set').all() if cleaned_data['name']: queryset = queryset.filter(Q(name__icontains=cleaned_data['name'])) @@ -622,7 +622,7 @@ class StockViewDetail(LoginRequiredMixin, BaseTemplateMixin, DetailView): class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView): template_name = 'parts/components-detail.html' - queryset = Component.objects.select_related('component_type', 'package', 'manufacturer').prefetch_related('componentparameter_set') + queryset = Component.objects.select_related('component_type', 'package', 'manufacturer').prefetch_related('componentparameter_set', 'distributornum_set') pk_url_kwarg = 'uuid' base_title = '' navbar_selected = 'Components' @@ -635,8 +635,8 @@ class ComponentDetailView(LoginRequiredMixin, BaseTemplateMixin, DetailView): context['comp_form'] = ComponentForm(instance=self.object) context['new_distri_num_form'] = DistributorNumberCreateForm() context['new_param_form'] = ComponentParameterCreateForm() - context['distri_nums'] = DistributorNum.objects.filter(component=self.object).order_by('distributor__name') - context['parameters'] = ComponentParameter.objects.filter(component=self.object).order_by('parameter_type__parameter_name') + context['distri_nums'] = self.object.distributornum_set.select_related('distributor').order_by('distributor__name') + context['parameters'] = self.object.componentparameter_set.order_by('parameter_type__parameter_name') if self.object.package: context['package_parameters'] = PackageParameter.objects.filter(package=self.object.package).order_by('parameter_type__parameter_name')