【问题标题】:How can I join two tables in Django and use the result in my template?如何在 Django 中加入两个表并在我的模板中使用结果?
【发布时间】:2015-01-11 10:30:18
【问题描述】:

我很难在 Django 1.7 中使用外键。供参考:

models.py 如下所示:

from django.db import models

class Transaction(models.Model):
    transaction_num = models.IntegerField(primary_key=True)
    customer_name = models.CharField(max_length=255, blank=True)
    transaction_date = models.DateField(blank=True, null=True)
    phone = models.CharField(max_length=255, blank=True)
    est_pickup_date = models.DateField(blank=True, null=True)

    class Meta:
        db_table = 'transaction'

        def __str__(self):              # __unicode__ on Python 2
            return str(self.transaction_num)

class Item(models.Model):
    itemid = models.IntegerField(primary_key=True)
    transaction_num = models.ForeignKey(Transaction, db_column='transaction_num')
    desc = models.CharField(max_length=255)
    picked_up_on = models.DateField(blank=True, null=True)

    class Meta:
        db_table = 'item'

    def __str__(self):              # __unicode__ on Python 2
        return str(self.itemid)

class TransactionNote(models.Model):
    noteid = models.IntegerField(primary_key=True)
    transaction_num = models.ForeignKey(Transaction, db_column='transaction_num')
    content = models.CharField(max_length=255)

    class Meta:
        db_table = 'transaction_note'

    def __str__(self):              # __unicode__ on Python 2
        return str(self.noteid)

views.py 如下所示:

from django.http import HttpResponse
from django.shortcuts import render
from pickup.models import Transaction, Item, TransactionNote

# Current Inventory
def history(request):
    t = Transaction.objects.all().order_by('-transaction_date', '-transaction_num')

    return render(request, 'pickup/history.html', { 'transaction_list': t})

...和 ​​history.html 的相关部分,一个循环内的表,我想在其中包括附加到特定事务​​的所有相关项目的计数:

{% for transaction in transaction_list %}

[...]

<td>{{ transaction.customer_name }}</td>
<td>{{ transaction.transaction_date }}</td>
<td>{{ transaction.est_pickup_date }}</td>
<td>{{ transaction.phone }}</td>
<td>{{ transaction.itemid.count }}</td>

[...]

{% endfor %}

除了最后一行之外,一切正常。我不知道如何加入 Transaction 和 Item 表,以便我可以在模板内使用相关的项目对象(特别是描述或它们的计数)。任何帮助将不胜感激。

【问题讨论】:

    标签: python mysql django foreign-keys


    【解决方案1】:

    Django 提供了查询对象的所有反向关系的能力。

    为了实现这一点,Django 为反向关系对象提供了所谓的“related_name”,它默认为附加了“_set”的类名。

    所以,你的代码应该是这样的,

    {% for item in transaction.item_set.all %}
    ... do stuff ...
    {% endfor %}
    

    {{ transaction.item_set.count }}
    

    这些都没有经过测试,但我相信它应该与此密切相关。

    【讨论】:

      【解决方案2】:

      你需要使用

      <td>{{ transaction.item_set.count }}</td>
      

      不是transaction.itemid.count}} 来获取交易项目的数量。 Django添加&lt;modelname&gt;_set成员反向访问外键字段。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-08-24
        • 2016-03-16
        • 1970-01-01
        • 2021-04-29
        • 2020-05-02
        • 2021-09-11
        • 2016-06-26
        相关资源
        最近更新 更多