【问题标题】:I want to display the name of the values in django anotate我想在 django annotate 中显示值的名称
【发布时间】:2020-01-26 22:07:59
【问题描述】:

我正在尝试建立一个类似于迷你银行的应用程序,我可以在其中进行存款和取款。在其中一个模板中,我想对具有相同货币名称的模型中的所有存款和取款进行汇总,因此我使用 annotate 可以正常工作。 但我无法在模板中显示货币名称。相反,它显示的是 1,2,3 而不是英镑、美元、欧元。

这是我模型的相关部分:

class Banks(models.Model):
    currency = models.ForeignKey(Currency, blank=True, null=True)
    total_deposits = models.IntegerField(blank=True, null=True)
    total_withdrawals = models.IntegerField(blank=True, null=True)

这里是视图:

result = Banks.objects.values('currency'
        ).order_by('currency'
        ).annotate(total_withdrawals=Sum('total_withdrawals')
        ).annotate(total_deposits=Sum('total_deposits')
        )

context = {
        "result": result,
        }

这是模板:

<table class='table'>
          <thead>
            <tr>
              <th class='aligncenter'>#</th>
              <th class='aligncenter'>CURRENCY</th>
              <th class='aligncenter'>TOTAL DEPOSITS</th>
              <th class='aligncenter'>TOTAL WITHDRAWALS</th>
              <th class='aligncenter'>BALANCE</th>
            </tr>
          </thead>
        {% for instance in result %}
            <tr>
              <td class='aligncenter'>{{forloop.counter}}</td>
              <td class='aligncenter'>{{instance.currency}}</td>
              <td class='aligncenter'>{{instance.total_deposits}}</td>
              <td class='aligncenter'>{{instance.total_withdrawals}}</td>
              <td class='aligncenter'>{{instance.total_deposits|sub:instance.total_withdrawals}}</td>
            </tr>
        {% endfor %}
 </table>

这是模板输出:

我想在货币列下查看货币名称。不是 1、2 和 3

【问题讨论】:

  • 你能分享Currency的模型吗?
  • {{instance.currency}} 返回货币 ID(我猜你会覆盖 Currency 模型中的 __str__ 方法来这样做)。相反,或者更改该模型的 __str__ 方法以返回您在那里为货币名称所拥有的任何字段,或者通过引用该字段 {{instance.currency.currency_name_field}} 在模板中执行相同操作
  • @Charnel:这是因为.values(..)
  • 是的,你是对的,因为它是一个 FK,它会返回一个整数。
  • @WillemVanOnsem:这是Currency 模型:class Currency(models.Model):name = models.CharField(max_length=200, default='', blank=True, null=True)symbol = models.CharField(max_length=200, default='', blank=True, null=True)def __unicode__(self):return self.symbol

标签: django django-models django-templates django-views


【解决方案1】:

您应该将货币名称添加到您的值中,您可以这样做:

from django.db.models import F

result = Banks.objects.values(
    'currency',
    currency_name=F('currency__name')
).order_by('currency', 'currency_name').annotate(
    total_withdrawals=Sum('total_withdrawals'),
    total_deposits=Sum('total_deposits')
)

(假设Currency 有一个name 字段,如果它有不同的字段,您可以用F('currency__<i>fieldname</i>') 替换它fieldname)。

然后你可以用:

&lt;td class='aligncenter'&gt;{{ instance.<b>currency_name</b> }}&lt;/td&gt;

话虽如此,由于您在这里按货币分组,因此简单地注释您的货币更有意义,例如:

result = Currency.objects.annotate(
    total_withdrawals=Sum('banks__total_withdrawals'),
    total_deposits=Sum('banks__total_deposits')
)

然后你可以渲染instance, which will call thestr(..)on theCurrency`对象,例如:

<td class='aligncenter'>{{forloop.counter}}</td>
<td class='aligncenter'>{{ instance }}</td>
<td class='aligncenter'>{{ instance.total_deposits }}</td>
<td class='aligncenter'>{{ instance.total_withdrawals }}</td>
<td class='aligncenter'>{{ instance.total_deposits|sub:instance.total_withdrawals }}</td>

【讨论】:

  • 我想这就是我要找的。但我收到了这个错误:/uimsapp/client_list/values() 的 TypeError 得到了一个意外的关键字参数 'currency_name'
  • @simplyvic:你是否使用了双下划线(__),Currency 模型是否有字段name
  • @willen Van Onsem:是的,我使用了双下划线,货币模型确实有一个字段名称。错误是抱怨第一个 currency_name,而不是括号中的那个。
  • @simplyvic:你使用什么版本的 Django? 1.10 和更早的版本确实不支持(尽管这些已经过时了)。
  • 哦,我使用的是 Django 1.8。那我需要升级。升级后我会回来更新你。
猜你喜欢
  • 2019-03-16
  • 1970-01-01
  • 1970-01-01
  • 2020-09-26
  • 1970-01-01
  • 2012-09-18
  • 1970-01-01
  • 1970-01-01
  • 2022-11-17
相关资源
最近更新 更多