在Django Tables2中,如何从外键引用的表中制作列显示文本?

发布于 2025-02-11 15:43:34 字数 2151 浏览 1 评论 0原文

在阅读了我能找到的所有文档和答案之后,燃烧了一整天,我仍然无法做这项工作。使用django tables2,我想显示一个乐器列表;仪器表中包含仪表型表的外键。当我列出仪器及其属性时,我想使用外键从另一个表代替文本仪器类型说明。我已经尝试了双重下划线和其他访问者技术的每种组合,但是到目前为止,我所得到的就是恐惧。 (仅显示记录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" )

涉及的模型是: instruments model.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           —

我发现的最相关的在线参考是在这里在这里;但是尝试了建议,没有运气。我想念什么?

After reading all the docs and answers I can find, and burning a whole day, I still can't make this work. Using Django Tables2, I want to show a list of instruments; the instruments table includes a foreign key to an instrumentsType table. When I list the instruments and their attributes, I want to use the foreign key to substitute the textual instrument type description from the other table. I have tried every combination of double underscores and other accessor techniques, but so far all I get is the dreaded -- in the column. (Displaying just the record ID works).

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" )

The models involved are:
Instruments model.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)

InstrumentTypes model.py

from django.db  import models

class InstrumentType(models.Model):
    instrumentType = models.CharField(max_length=40)

Resulting output:

ID  Instrument  Nickname    SerialNo    Instrumenttype
4   instr2       nock2       123           —

The most relevant online references I have found are here and here; but having tried the suggestions, no luck. What am I missing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

动次打次papapa 2025-02-18 15:43:34

我也一直在努力使某些工作有效(但我终于做到了),我发现这些例子太简短了。

我认为您想在Meta类中摆脱这些东西,

"instrument__instrumenttype_id__instrumenttypes__id_instrumentType" 

我认为meta.fields应该只是字段名称的列表,并且您从其他表中的属性参考您以后将传递到Entrumenttable构造函数的对象类型的视图(并且在Meta.model属性中命名:

from django_tables2.utils import Accessor

class InstrumentTable(tables.Table):
    instrument_type = tables.Column(accessor=Accessor('instrumenttype.name'))
            
    class Meta:
        model = Instrument
        template_name = "django_tables2/bootstrap.html"
        fields = ("id", "instrument", "nickname", "serialNo", "instrument_type")

然后,在视图中,使一个乐器实例

def myview(request):
    table_to_render = InstrumentTable(Instrument.objects)
    return render(request, sometemplate, {table: table_to_render})

您没有显示您的查看,我知道如果您在存储库中有整个东西,请留下链接。

I've been struggling to get something working too (but I finally did), and I found the examples too brief.

I think you want to get rid of this stuff in the Meta class

"instrument__instrumenttype_id__instrumenttypes__id_instrumentType" 

I think Meta.fields should just be a list of field names, and that you refer to the attribute in the other table from the point of view of the type of object you will later pass in to the IntrumentTable constructor (and that is named in the Meta.model attribute:

from django_tables2.utils import Accessor

class InstrumentTable(tables.Table):
    instrument_type = tables.Column(accessor=Accessor('instrumenttype.name'))
            
    class Meta:
        model = Instrument
        template_name = "django_tables2/bootstrap.html"
        fields = ("id", "instrument", "nickname", "serialNo", "instrument_type")

Then, in view, make an instance of InstrumentTable

def myview(request):
    table_to_render = InstrumentTable(Instrument.objects)
    return render(request, sometemplate, {table: table_to_render})

You didn't show your view, and I know there may be a different way. If you have the whole thing in a repo somewhere, leave a link.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文