python-在 Django Tables2 中,如何使列显示由外键引用的表中的文本?
发布时间:2022-07-02 23:52:37 391
相关标签: # node.js
在阅读了我能找到的所有文档和答案,并花了一整天的时间之后,我仍然无法完成这项工作。使用Django表2,我想显示一个仪器列表;instruments表包括InstrumentType表的外键。当我列出乐器及其属性时,我想使用外键替换另一个表中的文本乐器类型描述。我尝试了双下划线和其他访问器技术的每一种组合,但到目前为止,我得到的都是可怕的——在专栏中。(仅显示记录ID有效)。
from .models import Instrument
from django_tables2 import A
from instrumenttypes.models import InstrumentType
class InstrumentTable(tables.Table):
id = tables.LinkColumn('instrument_details', args=[A('station_id')])
class Meta:
model = Instrument
template_name = "django_tables2/bootstrap.html"
fields = ("id", "instrument", "nickname", "serialNo",
"instrument__instrumenttype_id__instrumenttypes__id_instrumentType" )
涉及的模型有:仪器模型。py公司
from django.db import models
from instrumenttypes.models import InstrumentType
from stations.models import Station
# Create your models here.
class Instrument(models.Model):
instrument = models.CharField(max_length=40)
instrumenttype = models.ForeignKey(InstrumentType, on_delete=models.CASCADE, null=True)
station = models.ForeignKey(Station, on_delete=models.CASCADE, default=1)
serialNo = models.CharField(max_length=60, null=True, blank=True)
dateAdded = models.DateTimeField("Date Added", null=True, blank=True)
dateRemoved = models.DateTimeField("Date Removed", null=True, blank=True)
status = models.CharField(max_length=10, null=True, blank=True)
nickname = models.CharField(max_length=40, null=True, blank=True)
仪器类型模型。py公司
from django.db import models
class InstrumentType(models.Model):
instrumentType = models.CharField(max_length=40)
结果输出:
ID Instrument Nickname SerialNo Instrumenttype
4 instr2 nock2 123 —
特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报