Skip to content

Commit a731041

Browse files
committed
Single query
1 parent 6215f6f commit a731041

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

src/staff/tables.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
ExtractionPlate,
1414
Order,
1515
Sample,
16-
SampleIsolationMethod,
1716
SampleMarkerAnalysis,
1817
)
1918
from nina.models import Project
@@ -457,10 +456,11 @@ class OrderAnalysisSampleTable(tables.Table):
457456
},
458457
)
459458

460-
isolation_method = tables.Column(
459+
isolation_method = tables.ManyToManyColumn(
460+
accessor="sample__isolation_method",
461+
transform=lambda x: x.name,
461462
verbose_name="Isolation Method",
462-
accessor="sample", # needed to get `record.sample` instead of `None`
463-
empty_values=(),
463+
orderable=False,
464464
)
465465

466466
class Meta:
@@ -488,13 +488,6 @@ def render_checked(self, record: SampleMarkerAnalysis) -> str:
488488
record.id,
489489
)
490490

491-
def render_isolation_method(self, record: SampleMarkerAnalysis) -> str:
492-
print(record)
493-
qs = SampleIsolationMethod.objects.filter(sample=record.sample).values_list(
494-
"isolation_method__name", flat=True
495-
)
496-
return ", ".join(qs)
497-
498491

499492
class PlateTable(tables.Table):
500493
id = tables.Column(

src/staff/views.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,14 @@ def get_order(self) -> ExtractionOrder:
559559
def get_queryset(self) -> QuerySet[Sample]:
560560
return Sample.objects.filter(
561561
order=self.get_order(), genlab_id__isnull=False
562-
).prefetch_related("order", "type", "isolation_method")
562+
).prefetch_related(
563+
"order",
564+
"type",
565+
Prefetch(
566+
"isolation_method",
567+
queryset=IsolationMethod.objects.order_by("name").distinct(),
568+
),
569+
)
563570

564571
def get_isolation_methods(self) -> QuerySet[IsolationMethod, str]:
565572
types = self.get_queryset().values_list("type", flat=True).distinct()

0 commit comments

Comments
 (0)