Django Tables2显示多个表 [英] Django Tables2 Display Multiple Tables
本文介绍了Django Tables2显示多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有与课程和注册记录相关的用户记录.我想单击一个用户,并在同一页面中查看来自用户表,课程表和注册表的原始数据.
I've got user records that have related(ish) course and enrollment records. I want to click on a user and see raw data from the user table, course table, and enrollment table in the same page.
当我尝试渲染表格时,该过程失败了.
The process breaks down when I attempt to render the tables.
views.py:
def explore_related(request, client_id, user_id):
client = get_object_or_404(Client, pk=client_id)
users = Users.objects.filter(pk=user_id)
enrollments = Enrollments.objects.filter(client_id=client_id).filter(userID__in=users.values_list('userID', flat=True)).all()
courses = Courses.objects.filter(client_id=client_id).filter(sectionSchoolCode__in=enrollments.values_list('sectionSchoolCode', flat=True)).all()
userTable = UserTable(users, prefix='u_')
courseTable = CourseTable(courses, prefix='c_')
enrollmentTable = EnrollmentTable(enrollments, prefix='e_')
payload = {
'userTable': userTable,
'enrollmentTable': enrollmentTable,
'courseTable': courseTable,
}
return render(request, 'importer/explore_related.html', payload)
explore_related.html:
explore_related.html:
{% load render_table from django_tables2 %}
<html>
<body>
{% render_table userTable %}
<br>
{% render_table courseTable %}
<br>
{% render_table enrollmentTable %}
</body>
</html>
tables.py
tables.py
class UserTable(tables.Table):
selection = tables.CheckBoxColumn(accessor='userID', orderable=False)
errors = tables.Column()
User_ID = tables.LinkColumn(
'importer:explore_related',
text=lambda record: record.userID,
kwargs={
'client_id': tables.A('client_id'),
'file_kind': 'user',
'object_id': tables.A('id'),
},
empty_values='NULL',
)
class Meta:
model = Users
attrs = {'class': 'paleblue'}
exclude = ['id', 'client', 'userID']
sequence = (
'selection',
'...',
'errors'
)
class CourseTable(tables.Table):
selection = tables.CheckBoxColumn(accessor='pk', orderable=False)
errors = tables.Column()
class Meta:
model = Courses
attrs = {'class': 'paleblue'}
exclude = ['id', 'client']
sequence = (
'selection',
'...',
'errors'
)
class EnrollmentTable(tables.Table):
selection = tables.CheckBoxColumn(accessor='pk', orderable=False)
errors = tables.Column()
class Meta:
model = Enrollments
attrs = {'class': 'paleblue'}
exclude = ['id', 'client']
sequence = (
'selection',
'...',
'errors'
)
推荐答案
如果使用自定义表类,则需要使用 RequestConfig 对象正确设置表.
If you use custom table classes, you need to use a RequestConfig object to properly set-up the table.
在您的示例中,添加就足够了
In your example, it should be enough to add
RequestConfig(request, paginate=False).configure(userTable)
RequestConfig(request, paginate=False).configure(courseTable)
RequestConfig(request, paginate=False).configure(enrollmentTable)
,然后将其添加到有效载荷.
这篇关于Django Tables2显示多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文