我有2個字段,總年度葉和年採取的葉子, 使用.aggregate(Sum())
我已經計算了一個僱員的年度總採伐量。蟒蛇 - 用.aggregate(Sum())結果減去字段值
,但現在我要計算用戶可用的,這是
年度總葉剩下的葉子 - 採取
年總葉子,但我無法這樣做,因爲annual_taken
是一個字符串,而不是浮動
它給我這個錯誤:
不支持的操作數類型爲 - :'float'和'str'
我該如何解決這個問題?任何幫助深表感謝
下面是我的代碼: model.py
class LeavesEntitlement(models.Model):
employee = models.ForeignKey(Employee, on_delete=models.CASCADE, related_name='+')
type = models.ForeignKey(LeavesType, on_delete=models.CASCADE, related_name='+')
days = models.FloatField(null=True, blank=True)
class Leave(models.Model):
employee = models.ForeignKey(Employee, on_delete=models.CASCADE, related_name='+')
type = models.ForeignKey(LeavesType, on_delete=models.CASCADE, related_name='+')
duration = models.FloatField()
View.py
def my_leaves_view(request):
annual = LeavesEntitlement.objects.all().filter(employee=request.user.profile.employee.id, type=1)
annual_taken = Leave.objects.all().filter(employee=request.user.profile.employee.id, type=1).aggregate(Sum('duration'))
for annual_field in annual:
for field in annual_taken:
annual_available = annual_field.days - field
context = {'form': form,
'annual': annual,
'annual_taken': annual_taken,
'annual_available': annual_available
}
return render(request, 'hrm/my_leaves/my_leaves_content.html', context)
HTML
<table id="my_entitlement_table" class="display table table-hover table-responsive" width="100%">
<thead>
<tr>
<th class="small text-muted text-uppercase"><strong>Leave Type</strong></th>
<th class="small text-muted text-uppercase"><strong>Total</strong></th>
<th class="small text-muted text-uppercase"><strong>Taken</strong></th>
<th class="small text-muted text-uppercase"><strong>Available</strong></th>
</tr>
</thead>
<tbody>
{% for annual_field in annual %}
<tr>
<td>{{annual_field.days}}</td>
<td>{{annual_taken.duration__sum}}</td>
<td>{{annual_available}}</td>
</tr>
{% endfor %
</tbody>
在您的問題中包含完整的追溯 –
好的,一會兒。 –