Sum of F() expression and timedelta created from F() expression

后端 未结 2 665
栀梦
栀梦 2021-01-14 00:47

My Proposal model is defined as follows:

class Proposal(models.Model):
    scheduled_time = models.DateTimeField()
    duration = models.IntegerField() # sto         


        
2条回答
  •  无人及你
    2021-01-14 01:16

    F() can't be used that way, as @Daniil already pointed out.

    A possible solution is to add a new field

    end_time = models.DateTimeField()
    

    and override the save method

    def save(self, *args, **kwargs):
        if not self.end_time:
            self.end_time = self.scheduled_time + datetime.timedelta(self.duration)
        super(Proposal, self).save(*args, **kwargs)
    

提交回复
热议问题