[英]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.我尝试在django-tables2
表中呈现django-simple-history
查询集。 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.此外,我想将查询集传递给Table
object 以使用表的功能,如链接列或排除列。 For this I have to specify a model inside the tables meta.为此,我必须在表 meta 中指定 model。 The problem here is, that the model for the history is generated automatically.这里的问题是,历史记录的 model 是自动生成的。
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
有没有办法引用自动生成的HistoryModel
Reading the docs helps some times.阅读文档有时会有所帮助。
https://django-simple-history.readthedocs.io/en/latest/common_issues.html#pointing-to-the-model 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
# ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.