Django-tables2多表数据展示问题:浓度显示异常与多控件拼接
问题2:拼接多个NegativeControl名称显示
controls是探针关联的NegativeControl集合,直接显示会输出QuerySet的默认字符串(也就是你看到的all.NegativeControl.None),需要自定义渲染逻辑把所有控件名称拼起来。
修改表格代码:
# all/tables.py class AllProbesTable(tables.Table): # 注意:原代码里Probe列的accessor是"probe.name",但每条record本身就是Probe实例,直接用"name"即可 Probe = tables.Column(accessor="name", linkify=True) control = tables.Column(empty_value="无") mechanismOfAction = tables.Column() # 按问题1的方案配置cellularUsageConc列 def render_control(self, value, record): # 提取所有关联控件的名称并拼接 control_names = record.controls.values_list('name', flat=True) return ', '.join(control_names) if control_names else "无" class Meta: template_name = "django_tables2/bootstrap5-responsive.html"
另外确保视图里的prefetch_related已经包含controls和usageData,避免重复查询数据库:
# all/views.py class AllProbesView(SingleTableView): table_class = AllProbesTable queryset = Probe.objects.all().prefetch_related('usageData', 'controls') template_name = "all/probe_list.html"
内容的提问来源于stack exchange,提问作者Natalie
相关产品推荐
相关产品推荐

