Improve a few queries

This commit is contained in:
Mario Hüttel 2022-01-11 20:45:01 +01:00
parent a566e198b8
commit 009ff5ae96

View File

@ -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')