【问题标题】:Render a django-simple-history query in a django-tables2 table在 django-tables2 表中呈现 django-simple-history 查询
【发布时间】:2020-07-03 12:37:01
【问题描述】:

我尝试在django-tables2 表中呈现django-simple-history 查询集。目前我将原始查询集传递给上下文中的模板。此外,我想将 Queryset 传递给 Table 对象以使用表的功能,如 linkyfy 列或排除列。为此,我必须在表元中指定一个模型。这里的问题是,历史模型是自动生成的。

实际代码:

#views.py
from .tables import HistoryTable

class HistoryView(LoginRequiredMixin, SingleTableView):
    template_name = "funkwerkstatt/history.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["table"] = Device.history.filter(id=self.kwargs["pk"])
        ## futur code
        #context["table"] = HistoryTable(Device.history.filter(id=self.kwargs["pk"]))
        return context

#tables.py
import django_tables2 as tables

class HistoryTable(tables.Table):
    device = tables.Column(accessor="device", linkify=True)
    class Meta:
        model = # HistoryModel?!
        empty_text = "No entry"
        exclude = ["id",]

有没有办法引用自动生成的HistoryModel

【问题讨论】:

    标签: python django django-tables2 django-simple-history


    【解决方案1】:

    阅读文档有时会有所帮助。

    https://django-simple-history.readthedocs.io/en/latest/common_issues.html#pointing-to-the-model

    class PollHistoryListView(ListView): # or PollHistorySerializer(ModelSerializer):
        class Meta:
            model = Poll.history.model
           # ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-23
      • 1970-01-01
      • 2021-03-26
      • 2014-08-04
      • 2020-09-29
      • 2020-05-05
      相关资源
      最近更新 更多