如何使用 django-tables2(自定义 table)在 table 单元格中显示来自外部 table 的数据?
How can I display the data from a foreign table in a table cell using django-tables2 (custom table)?
table 单元格中的值为空。此列包含来自国外的数据 table.
这是我的模型片段。
model.py
class DailyRecord(models.Model):
date_organised = models.DateField('Ogransied Date', help_text=('Enter Date when the program is organised: CCYY-MM-DD'))
program_name = models.TextField('program name',)
venue = models.CharField('venue', max_length = 255, blank=True)
organiser = models.ForeignKey(Organiser, verbose_name = 'Organiser', related_name = 'organisers')
objects = models.Manager()
public = DailyRecordManager()
class Meta:
verbose_name = 'dailyrecord'
verbose_name_plural = 'dailyrecords'
ordering = ['-date_organised']
def __str__(self):
return self.program_name.encode('ascii', errors='replace')
class Participant(models.Model):
participant = models.CharField(max_length= 50)
daily_record = models.ForeignKey(DailyRecord, verbose_name = 'program_name', related_name = 'participant_set')
class Meta:
verbose_name = 'participant'
verbose_name_plural = 'participants'
def __str__(self):
return self.participant.encode('ascii', errors='replace')
这是 table 上的 class 以显示自定义 table。下面是片段
tables.py
class DailyRecordTable(tables.Table):
date_organised = tables.Column('Date')
program_name = tables.Column( 'Program Name')
venue = tables.Column('Venue')
organiser = tables.Column( 'Organiser')
participant = tables.Column(accessor='participant.participant')
class Meta:
model = DailyRecord
这是我用于显示 table 的通用视图。
views.py
class DailyActivityPageView(SingleTableView):
queryset = DailyRecord.public.prefetch_related('participant_set').all()
table = DailyRecordTable(queryset)
template_name = 'dailyrecord/daily-activity-record.html'
def get(self, request):
RequestConfig(request).configure(self.table)
return render(request, self.template_name, {'table': self.table, 'ziptable':self.ziptable,'data' : self.data})
data.html
<tbody>
{% for row in table.page.object.list|default:table.rows %} {# support pagination #}
{% block table.tbody.row %}
<tr {{ row.attrs.as_html }}>
{% for column, cell in row.items %}
<td {{ column.attrs.td.as_html }}>
{{cell}}
{% if column.localize == None %}
{% if column.header == 'Participant' %}
{{cell}}
{% for item in cell.participant_set.all %}
{{item.participant}}
{% endfor %}
{% else %}
{{ cell}}
{% endif %}
{% else %}
{% if column.localize %}
{{ cell|localize }}
{% else %}
{{cell|unlocalize}}
{% endif %}
{% endif %}
</td>
{% endfor %}
</tr>
{% endblock table.tbody.row %}
{% empty %}
{% if table.empty_text %}
{% block table.tbody.empty_text %}
{% endblock table.tbody.empty_text %}
{% endif %}
{% endfor %}
</tbody>
输出screenshot
参与者栏为空。为什么?
您定义的访问器 'participant.participant'
是错误的:DailyRecord
没有 participant
字段。它在 participant_set
returning Participant
模型实例中确实有一个 Manager
,参见 Django documentation on making queries "Following relationships 'backward'"
仍然,它将 return 一个 QuerySet 而不是单个值。您需要告诉 django-tables2 忽略空值并无论如何显示该列。当然,如果没有为 DailyRecord 定义参与者,您必须确保您的模板也能正常工作:
template = '''
{% for item in record.participant_set.all %}
{{ item.participant }}
{% endfor %}
'''
class DailyRecordTable(tables.Table):
date_organised = tables.Column('Date')
program_name = tables.Column( 'Program Name')
venue = tables.Column('Venue')
organiser = tables.Column( 'Organiser')
participant = tables.TemplateColumn(empty_values=(), template_code=template)
class Meta:
model = DailyRecord
table 单元格中的值为空。此列包含来自国外的数据 table.
这是我的模型片段。
model.py
class DailyRecord(models.Model):
date_organised = models.DateField('Ogransied Date', help_text=('Enter Date when the program is organised: CCYY-MM-DD'))
program_name = models.TextField('program name',)
venue = models.CharField('venue', max_length = 255, blank=True)
organiser = models.ForeignKey(Organiser, verbose_name = 'Organiser', related_name = 'organisers')
objects = models.Manager()
public = DailyRecordManager()
class Meta:
verbose_name = 'dailyrecord'
verbose_name_plural = 'dailyrecords'
ordering = ['-date_organised']
def __str__(self):
return self.program_name.encode('ascii', errors='replace')
class Participant(models.Model):
participant = models.CharField(max_length= 50)
daily_record = models.ForeignKey(DailyRecord, verbose_name = 'program_name', related_name = 'participant_set')
class Meta:
verbose_name = 'participant'
verbose_name_plural = 'participants'
def __str__(self):
return self.participant.encode('ascii', errors='replace')
这是 table 上的 class 以显示自定义 table。下面是片段
tables.py
class DailyRecordTable(tables.Table):
date_organised = tables.Column('Date')
program_name = tables.Column( 'Program Name')
venue = tables.Column('Venue')
organiser = tables.Column( 'Organiser')
participant = tables.Column(accessor='participant.participant')
class Meta:
model = DailyRecord
这是我用于显示 table 的通用视图。
views.py
class DailyActivityPageView(SingleTableView):
queryset = DailyRecord.public.prefetch_related('participant_set').all()
table = DailyRecordTable(queryset)
template_name = 'dailyrecord/daily-activity-record.html'
def get(self, request):
RequestConfig(request).configure(self.table)
return render(request, self.template_name, {'table': self.table, 'ziptable':self.ziptable,'data' : self.data})
data.html
<tbody>
{% for row in table.page.object.list|default:table.rows %} {# support pagination #}
{% block table.tbody.row %}
<tr {{ row.attrs.as_html }}>
{% for column, cell in row.items %}
<td {{ column.attrs.td.as_html }}>
{{cell}}
{% if column.localize == None %}
{% if column.header == 'Participant' %}
{{cell}}
{% for item in cell.participant_set.all %}
{{item.participant}}
{% endfor %}
{% else %}
{{ cell}}
{% endif %}
{% else %}
{% if column.localize %}
{{ cell|localize }}
{% else %}
{{cell|unlocalize}}
{% endif %}
{% endif %}
</td>
{% endfor %}
</tr>
{% endblock table.tbody.row %}
{% empty %}
{% if table.empty_text %}
{% block table.tbody.empty_text %}
{% endblock table.tbody.empty_text %}
{% endif %}
{% endfor %}
</tbody>
输出screenshot
参与者栏为空。为什么?
您定义的访问器 'participant.participant'
是错误的:DailyRecord
没有 participant
字段。它在 participant_set
returning Participant
模型实例中确实有一个 Manager
,参见 Django documentation on making queries "Following relationships 'backward'"
仍然,它将 return 一个 QuerySet 而不是单个值。您需要告诉 django-tables2 忽略空值并无论如何显示该列。当然,如果没有为 DailyRecord 定义参与者,您必须确保您的模板也能正常工作:
template = '''
{% for item in record.participant_set.all %}
{{ item.participant }}
{% endfor %}
'''
class DailyRecordTable(tables.Table):
date_organised = tables.Column('Date')
program_name = tables.Column( 'Program Name')
venue = tables.Column('Venue')
organiser = tables.Column( 'Organiser')
participant = tables.TemplateColumn(empty_values=(), template_code=template)
class Meta:
model = DailyRecord