SQL efficiency on asset list

This commit is contained in:
2021-02-05 00:58:30 +00:00
parent baa3b2c9c6
commit c090163f40

View File

@@ -38,12 +38,12 @@ class AssetList(LoginRequiredMixin, generic.ListView):
# TODO Feedback to user when search fails # TODO Feedback to user when search fails
query_string = form.cleaned_data['q'] or "" query_string = form.cleaned_data['q'] or ""
if len(query_string) == 0: if len(query_string) == 0:
queryset = self.model.objects.all() queryset = self.model.objects.all().select_related('category', 'status')
elif len(query_string) >= 3: elif len(query_string) >= 3:
queryset = self.model.objects.filter( queryset = self.model.objects.filter(
Q(asset_id__exact=query_string) | Q(description__icontains=query_string) | Q(serial_number__exact=query_string)) Q(asset_id__exact=query_string) | Q(description__icontains=query_string) | Q(serial_number__exact=query_string)).select_related('category', 'status')
else: else:
queryset = self.model.objects.filter(Q(asset_id__exact=query_string)) queryset = self.model.objects.filter(Q(asset_id__exact=query_string)).select_related('category', 'status')
if form.cleaned_data['category']: if form.cleaned_data['category']:
queryset = queryset.filter(category__in=form.cleaned_data['category']) queryset = queryset.filter(category__in=form.cleaned_data['category'])