Django-tables2: How to use accessor to bring in foreign columns?

只愿长相守 提交于 2019-12-04 20:16:01

问题


I've tried reading the docs and previous answers to this question without much luck.

I've got a bunch of student-course registrations and I'd like to see some of those selected registrations in conjunction with some of the attributes of the students. No luck so far...I'd request your advice!

Here's the model:

class Student(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    netID = models.CharField(max_length=8)

class Registration(models.Model):
    student = models.ForeignKey(Student)
    course = models.ForeignKey(Course)
    attendance_M = models.BooleanField(default=False)
    attendance_Tu = models.BooleanField(default=False)

and here is the tables.py:

class AttendanceTable(tables.Table):
    netID = tables.Column(accessor='Student.netID')
    first = tables.Column(accessor='Student.first_name')
    last = tables.Column(accessor='Student.last_name')
    class Meta:
        model = Registration
        attrs = {"class": "paleblue"}
        fields = ('attendance_M', 'attendance_Tu',)
        sequence = ('netID', 'first', 'last', 'attendance_M', 'attendance_Tu',)

While I'm getting data on the attendance values, there's nothing from the student foreign columns.

netID   First   Last    Attendance M    Attendance Tu
 —         —      —      ✔               ✘ 

And it's the same deal if I start the Table with model = Student and use accessors against the Registration table, it's the same deal.

I feel like I'm missing something very conceptual and crucial -- please guide me!


回答1:


The model name in the accessor parameter of the column should be lowercase.

Use accessor='student.netID' instead of accessor='Student.netID'.



来源:https://stackoverflow.com/questions/20777125/django-tables2-how-to-use-accessor-to-bring-in-foreign-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!