15

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!

4

2 回答 2

18

列的访问器参数中的模型名称应为小写。

使用accessor='student.netID'而不是accessor='Student.netID'.

于 2015-11-11T23:29:47.417 回答
1

使用该accessor参数时,您必须使用具有外键的模型中声明的字段名称,然后从该表中选择您要使用的字段。

因此,对于这些模型:

#models.py
class Description_M(models.Model):
   id_hash = models.CharField(db_column='Id_hash', primary_key=True, max_length=22)
   description = models.CharField(db_column='Description', max_length=255, blank=True, null=True)

class GeoCodes(models.Model):
    geo = models.CharField(db_column='Geo', primary_key=True, max_length=5)
    city_name = models.CharField(db_column='City', max_length=150, blank=True, null=True)

class RefSources(models.Model):
    id_source   = models.IntegerField(db_column='Id_source', primary_key=True,)  
    source_name   = models.CharField(db_column='Source', max_length=150, blank=True, null=True)  

class Price(models.Model):
    id_hash = models.ForeignKey(Description_M, models.DO_NOTHING, db_column='Id_hash')
    date= models.ForeignKey(DateTime, models.DO_NOTHING, db_column='Date')
    geo = models.ForeignKey(GeoCodes, models.DO_NOTHING, db_column='Geo')
    id_source = models.ForeignKey(RefSources, models.DO_NOTHING, db_column='Id_source')  # Field name made lowercase.
    price = models.FloatField(db_column='Price',primary_key=True, unique=False,default=None)

使用外键从该表中提取字段时,您必须:

class price_table(tables.Table):
    description = tables.Column(accessor = 'id_hash.description')
    city = tables.Column(accessor = 'geo.city_name')
    source = tables.Column(accessor = 'id_source.source_name')
    class Meta:
        model = Price
        fields = ['date','price']
        sequence = ['description ','date','city ','source','price']
        template_name = 'django_tables2/bootstrap.html'
于 2021-05-05T16:42:03.993 回答