希望基于另一个字段创建默认值,类似于:
class Track(models.Model):
distance = models.FloatField()
timeout = models.PositiveSmallIntegerField(default=int(distance * 2))
然而这抛出TypeError: unsupported operand type(s) for *: 'FloatField' and 'int'
FWIW,我试过这个无济于事:
class Track(models.Model):
distance = models.FloatField()
timeout = models.PositiveSmallIntegerField()
def save(self, *args, **kwargs):
if (self.timeout is None):
self.timeout = int(self.distance * 2)
super(Track, self).save(*args, **kwargs)
但那是扔TypeError: 'int' object is not iterable