有没有一种方法可以在Django中添加模型值?

时间:2019-06-07 15:22:28

标签: python django

我想将所有天数总计。我真的不知道如何添加它们并存储总值。 有谁知道我该怎么解决?

这是我的模特:

class Weekstaat(models.Model):
    id = models.AutoField(primary_key=True, null=False, blank=False)
    status = models.CharField(max_length=11, choices=STATUS_CHOICES, default='Niet ingediend',)
    jaar = models.ForeignKey(Jaar, default=datetime.now().year, null=False, blank=False,on_delete=models.CASCADE)
    week = models.ForeignKey(Week, default=date.today().isocalendar()[1], null=False, blank=False,on_delete=models.CASCADE)
    werknemer = models.ForeignKey(User, related_name='weekstaat_werknemer', null=False, blank=False, default=1,on_delete=models.CASCADE)
    #contract = models.ForeignKey(Contract, null=False, blank=False, default=1,on_delete=models.CASCADE)
    maandag = models.FloatField(null=True, blank=True, default=0)
    dinsdag = models.FloatField(null=True, blank=True, default=0)
    woensdag = models.FloatField(null=True, blank=True, default=0)
    donderdag = models.FloatField(null=True, blank=True, default=0)
    vrijdag = models.FloatField(null=True, blank=True, default=0)
    zaterdag = models.FloatField(null=True, blank=True, default=0)
    zondag = models.FloatField(null=True, blank=True, default=0)

    def __str__(self):
        return str(self.jaar) + ' week ' + str(self.week) + str(self.werknemer) + str(self.status)

这是html:

                  <td class="text-center">
                    {{ Weekstaat.maandag }}
                  </td>
                  <td class="text-center">
                    {{ Weekstaat.dinsdag }}
                  </td>
                  <td class="text-center">
                    {{ Weekstaat.woensdag }}
                  </td>
                  <td class="text-center">
                    {{ Weekstaat.donderdag }}
                  </td>
                  <td class="text-center">
                    {{ Weekstaat.vrijdag }}
                  </td>
                  <td class="text-center">
                    {{ Weekstaat.zaterdag }}
                  </td>
                  <td class="text-center">
                    {{ Weekstaat.zondag }}
                  </td>
                  <td class="text-center"> 
                   ...................
                  </td>

1 个答案:

答案 0 :(得分:1)

我不清楚您要问什么,但也许可以使用一种简单的模型方法:

class Weekstaat(models.Model):
    ...

    def total(self):
        return sum([self.maandag, self.dinsdag, self.woensdag, self.donderdag, self.vrijdag, self.zaterdag, self.zondag])

然后您可以做:

{{ weekstaat.total }}