我有一个rake test
表,其中Compounds
字段与另一个名为name
的表相关联。
当我使用django-tables2呈现一个表格时,它显示得很好,除了它在Names
列中没有说出aspirin
的事实,它说name
。
Name object
:
models.py
class Compound(models.Model):
drug_id = models.AutoField(primary_key=True)
drug_name = models.ForeignKey(Name, db_column='drug_name', null=True, on_delete=models.PROTECT)
# for flagging problematic data
flag_id = models.ForeignKey(Flag, db_column='flag_id', null=True, on_delete=models.PROTECT)
# is a cocktail
is_combination = models.BooleanField()
class Meta:
db_table = 'compounds'
:
tables.py
import django_tables2 as tables
from .models import Compound
class FimTable(tables.Table):
drug_name = tables.Column(accessor='name.name')
class Meta:
model = Compound
attrs = {'class': 'paleblue table table-condensed table-vertical-center'}
fields = ('drug_id', 'drug_name')
sequence = ('drug_id', 'drug_name')
order_by = ('drug_id')
:
views.py
结果:
答案 0 :(得分:1)
您只需要在Name对象上定义__str__
方法。
class Name(models.Model):
...
def __str__(self):
return self.name
答案 1 :(得分:0)
你也可以使用......
class Name(model.Model):
...
def __unicode__(self):
return self.name