[英]can i make a two dimensional table in django-tables2?
Edited: Hi guys, I have been looking for a solution to my problem for some days without an answer I am trying to make a two-dimensional table with data obtained from the same model. 编辑:大家好,我几天来一直在寻找解决问题的方法,但没有答案,我试图用从同一模型获得的数据制作二维表。 The idea is to list the students in rows, the data in columns and the status in their respective cells, a two dimensional table. 这个想法是按行列出学生,按列列出数据,并在各自的单元格中列出状态,这是一个二维表。
class DailyAttendanceStudent(models.Model):
ATTENDANCE_CHOICES = (
(None,''),
(True,'Presente'),
(False, 'Ausente')
)
date = models.DateField(default=datetime.datetime.now)
status = models.NullBooleanField(choices=ATTENDANCE_CHOICES)
student = models.ForeignKey('perfiles.Student')
These are my table: 这些是我的桌子:
class StudentAttendanceTable(tables.Table):
nombres = tables.Column('nombres', accessor='Student.first_name')
apellidos = tables.Column('apellidos', accessor='Student.last_name')
date = tables.Column('fecha', accessor='date')#LinkColumn
status = tables.Column('status', accessor='status')
class Meta:
model = DailyAttendanceStudent
fields = ('nombres', 'apellidos', 'date', 'status')
graphically this is what I want to do: 以图形方式这就是我想要做的:
I think I would do something like this: 我想我会做这样的事情:
DailyAttendanceStudent
queryset like desired, and pass it to your table. 根据需要过滤DailyAttendanceStudent
,并将其传递到表中。 table.Column
, or something specialized to suit your needs. 新列可以是table.Column
,也可以是满足您需求的特殊列。 extra_columns
. 定制构造函数应调用父类的构造函数,将OrderedDict的各项作为数据传递,将date列作为extra_columns
。 In code, it could look like this: 在代码中,它可能看起来像这样:
from collections import OrderedDict
import django_tables2 as tables
class StudentAttendanceTable(tables.Table):
nombres = tables.Column('nombres', accessor='student.first_name')
apellidos = tables.Column('apellidos', accessor='student.last_name')
def __init__(self, data, *args, **kwargs):
rows = OrderedDict()
extra_columns = {}
for row in data:
if row.student.id not in rows:
rows[row.student.id] = {'student': row.student}
rows[row.student.id][row.date] = row.status
extra_columns[row.date.isoformat()] = tables.Column() # use more specialized column if you get this to work
super(StudentAttendanceTable, self).__init__(data=rows.values(), extra_columns=extra_columns.items(), *args, **kwargs)
You might want to sort the value you pass to extra_columns
, as the order retrieved from the database might not be the desired order for presentation. 您可能需要对传递给extra_columns
的值进行排序,因为从数据库检索的顺序可能不是表示所需的顺序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.