You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 21:50:21