pythondjangodjango-tables2django-simple-history

Render a django-simple-history query in a django-tables2 table


I try to render a django-simple-history queryset in a django-tables2 table. At the moment I pass the raw queryset to the template inside the context. Further I want to pass the Queryset to a Table object to use the features of the table like linkyfy columns or exclude columns. For this I have to specify a model inside the tables meta. The problem here is, that the model for the history is generated automatically.

Actually code:

#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",]

Is there a way to referent the auto generated HistoryModel


Solution

  • Reading the docs helps some times.

    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
           # ...