テーブルに計算列を作成しようとすると、一致するuser_idを持つすべてのdb行のallocated_hoursの合計が返されます。現在、各項目の列に「0」が表示されています。私はここで何が欠けていますか?助けてくれてありがとう。別の共通値を持つすべてのモデルオブジェクトの合計を計算する方法
私はdjango tables2を使用しています。
#model.py
class Allocation(models.Model):
user_id = models.ForeignKey(Resource)
project_id = models.ForeignKey(Project)
week = models.DateField(default=timezone.now)
allocated_hours = models.IntegerField(default=0)
actual_hours = models.IntegerField(default=0)
def __str__(self):
return '%s - %s' % (self.user_id, self.project_id)
def allocation_total(self):
alltotal = 0
for i in Allocation.objects.values_list('user_id'):
if i == Allocation.user_id:
alltotal += Allocation.allocated_hours
return alltotal
-
#tables.py
class Project_Resource_table(tables.Table):
role = tables.Column(accessor='user_id.resource_type')
name = tables.Column(accessor='user_id.resource_name')
allocation = tables.Column(accessor='allocation_total')
class Meta:
model = Allocation
exclude = ('id', 'user_id', 'project_id', 'week', 'allocated_hours', 'actual_hours')
sequence = ('role', 'name', 'allocation')
あなたは 'allocation_total'の定義で' Allocation.'の代わりに 'self.'を使ってみるべきです –
これは意味がありますが、私はこのコードを使用すると0よりむしろ最後になります: def allocation_total : alltotal = 0 for i in self.objects.values_list( 'user_id'): if i == self.user_id: alltotal + = self.allocated_hours リターンalltotal – jonin