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

时间:2022-07-12 11:59:27

The value on the table cell is empty. This column contains the data from foreign table.

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

Here is the snippet of my models.

这是我的模型的片段。

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

This is class on table to display custom table. below is the snippet

这是表上的类来显示自定义表。以下是片段

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

This is my Generic Views for displaying 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>

Output screenshot

The participant column is empty. Why?

参与者列为空。为什么?

1 个解决方案

#1


0  

The accessor you define 'participant.participant' is wrong: DailyRecord doesn't have a participant field. It does have a Manager in participant_set returning Participant model instances, see Django documentation on making queries "Following relationships 'backward'"

您定义'participant.participant'的访问者是错误的:DailyRecord没有参与者字段。它确实在participant_set中有一个管理器返回参与者模型实例,请参阅关于进行查询的Django文档“跟随关系'向后'”

Still, it will return a QuerySet and not a single value. You need to tell django-tables2 to ignore empty values and display the column anyway. Of course, you must make sure you template also works in case no participants are defined for a DailyRecord:

但是,它将返回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

#1


0  

The accessor you define 'participant.participant' is wrong: DailyRecord doesn't have a participant field. It does have a Manager in participant_set returning Participant model instances, see Django documentation on making queries "Following relationships 'backward'"

您定义'participant.participant'的访问者是错误的:DailyRecord没有参与者字段。它确实在participant_set中有一个管理器返回参与者模型实例,请参阅关于进行查询的Django文档“跟随关系'向后'”

Still, it will return a QuerySet and not a single value. You need to tell django-tables2 to ignore empty values and display the column anyway. Of course, you must make sure you template also works in case no participants are defined for a DailyRecord:

但是,它将返回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