【问题标题】:Django many-to-many field in template and through模板中的Django多对多字段并通过
【发布时间】:2018-08-25 10:28:40
【问题描述】:

在我的模型中有一些多对多字段。我一直在努力让它们出现在模板中。一个是常规的 ManyToMany 字段,另一个使用 through。问题是没有显示金额。我知道目前迭代仅在 pcbuilds.components.all 中为 component 定义。如何管理那里的金额?

models.py:

class Component(models.Model):
    name = models.CharField(max_length=100, unique=True,help_text='Component name')
    manufacturer = models.IntegerField(blank=True,null=True,choices=MANUFACTURERS)
    model = models.CharField(max_length=100,unique=True,blank=True,null=True, help_text='model')

class Tag(models.Model):
title = models.CharField(max_length=100,unique=True,blank=True,null=True, help_text='tagname')


class PCBuilds(models.Model):
    title = models.CharField(max_length=50, help_text='PC build title')
    components = models.ManyToManyField(Component,help_text='Pick your components from the list or create and add them.',through='Componentlist')
    subtitle = models.CharField(blank=True,max_length=80, help_text='Subtitle')

def __str__(self):
    return self.title

class Componentlist(models.Model):
    component = models.ForeignKey(Component, on_delete=models.CASCADE,related_name='components')
    pcbuild = models.ForeignKey(PCBuilds, on_delete=models.CASCADE,related_name='pcbuilds')
    amount = models.FloatField(null=True,help_text='amount')

模板:

<div class="card-deck">
    {% for pcbuilds in object_list|slice:":3" %}
        <div class="card" style="width: 18rem;">
            <div class="card-header">
                <a href="{% url 'pcbuilds_detail' pcbuilds.pk %}">
                <span class="font-weight-bold">{{ pcbuilds.title }}</span></a> &middot;
                <span class="text-muted">{{ pcbuilds.subtitle }}</span>
            </div>
            <div class="card-body">
            <ul>
            {% for component in pcbuilds.components.all %}
                   <li>{{ component.name}}{{ component.manufacturer}}{{ component.model }}{{ componentlist.amount }}</li>
            {% endfor %}
            </ul>
            </div>
            <div class="card-footer text-center text-muted">
            {% for tag in recipe.tags.all %}
            Tags: {{ tag.title }}
            {% endfor %}
            </div>
        </div>

        <br />
{% endfor %}

【问题讨论】:

    标签: django django-models django-templates many-to-many


    【解决方案1】:

    你没有在那里定义componentlist,所以模板会忽略它。通常,您需要遵循关系,就像您首先获得组件一样。但是这种方式无法访问直通表,因为您已经有效地通过它来获取目标表。

    相反,您需要遵循从 pcbuild 到直通表以及从那里到组件的关系:

     {% for componentlist in pcbuilds.pcbuilds.all %}
               <li>{{ componentlist.component.name}}{{ componentlist.component.manufacturer}}{{ componentlist.component.model }}{{ componentlist.amount }}</li>
      {% endfor %}
    

    注意,您在该表格中的相关名称很奇怪,这就是为什么我不得不使用那个令人困惑的pcbuilds.pcbuilds.all。 pcbuild到componentlist的反向关系应该是componentlist_set,这是默认的;应该没有任何理由改变这一点。

    【讨论】:

    • 知道了,现在删除相关名称并将 pcbuilds 更改为 compentlist_set。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2014-05-25
    • 2011-03-25
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 2012-03-29
    • 2017-09-19
    • 1970-01-01
    相关资源
    最近更新 更多