Python Django-tables2:如何使用访问器引入外部列?
我已经试着阅读了文档和之前的问题,但运气不好 我有一大堆学生课程注册,我想看看这些注册中的一些,以及学生的一些属性。到目前为止运气不好…我想征求你的意见 模型如下:Python Django-tables2:如何使用访问器引入外部列?,python,django,django-tables2,Python,Django,Django Tables2,我已经试着阅读了文档和之前的问题,但运气不好 我有一大堆学生课程注册,我想看看这些注册中的一些,以及学生的一些属性。到目前为止运气不好…我想征求你的意见 模型如下: 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 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)
下面是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',)
虽然我得到了出勤率的数据,但学生外国专栏中没有任何内容
netID First Last Attendance M Attendance Tu
— — — ✔ ✘
如果我以model=Student开始表,并对注册表使用访问器,这是相同的交易
我觉得我错过了一些非常概念性和关键性的东西——请指导我 列的访问器参数中的模型名称应为小写 使用accessor='student.netID'而不是accessor='student.netID'。使用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'
不应该是accessor='student.netID',student.first\u name,student.last\u name小写studentAha!这是正确的。非常感谢。