擅长:python、mysql、java
<p>您的代码看起来是正确的。如果你想为某些特定的目的节省时间,那没关系,你可以这样做。有一种更好的方法,您不必在运行时保存持续时间并派生数据</p>
<pre><code>class CarEventsData(models.Model):
car = models.ForeignKey(Car, on_delete=models.CASCADE, related_name="events_of_cow")
out = models.DateTimeField(null=True, help_text="When the cow went OUT TO Pasture.")
_in = models.DateTimeField(null=True, help_text="When the cow got back IN FROM Pasture.")
def duration(self):
return self.out - self._in
</code></pre>