【发布时间】:2018-05-11 03:22:54
【问题描述】:
我不知道如何在标题上说。但在这里检查一下:
我的model.py
class Employee(models.Model):
name = models.CharField(max_length=100)
class EmpLoan(models.Model):
status = models.BooleanField('Status', default=False)
nominal = models.DecimalField(max_digits=10, decimal_places=0)
employee = models.ForeignKey(Employee, null=True, on_delete=models.SET_NULL, related_name='emploan')
class EmpInstallment(models.Model):
nominal = models.DecimalField(max_digits=10, decimal_places=0)
loan = models.ForeignKey(EmpLoan, related_name='empinstallment')
created_at = models.DateTimeField(auto_now=True)
还有我的views.py
class EmpLoanListView(ListView):
context_object_name = 'emploans'
model = models.EmpLoan
def get_context_data(self, **kwargs):
context = super(EmpLoanListView, self).get_context_data(**kwargs)
context['form'] = EmpLoanForm()
return context
这里是我的template_loan.html
{% for emploan in emploans %}
<tr>
<td>{{ emploan.employee.name }}</td>
<td>{{ emploan.nominal|intcomma }},-</td>
<td>{{ emploan.installment.nominal|intcomma }},-</td>
</tr>
{% endfor %}
我在这里尝试使用emploan.installment.nominal 访问来自EmpInstallment 模型的数据,但它不起作用。
在我的情况下,如何从 ForeignKey 和 sum 获取数据?...
它将返回 get() 返回多个 ContentType -- 它返回 2!因为会有多个值,所以我也需要sum。
【问题讨论】: