如何使用django-tables2(自定义表)显示表格单元格中外表的数据?

时间:2017-08-03 09:41:57

标签: django django-templates django-tables2

表格单元格上的值为空。此列包含来自外表的数据。

以下是我模特的片段。

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')

这是表上显示自定义表的类。下面是摘录

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

这是我用于显示表格的通用视图。

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

参与者列为空。为什么呢?

1 个答案:

答案 0 :(得分:0)

您定义的访问者'participant.participant'错误:DailyRecord没有participant字段。它在[{1}}个Manager模型实例中有participant_set,请参阅Django documentation on making queries "Following relationships 'backward'"

但是,它将返回QuerySet而不是单个值。你需要告诉django-tables2忽略空值并显示列。当然,如果没有为DailyRecord定义参与者,您必须确保模板也有效:

Participant