我在模型中尝试过(使用" def save")但是给我错误" int()参数必须是字符串,类似字节的对象或数字,而不是&#39 ;长度'"
我有字段"长度"和"宽度"在外键。我想在提交表格时增加" squaremeter"字段到DB根据选择的长度和宽度计算
def save(self, *args, **kwargs):
self.squaremeter = self.length * self.width
self.squaremeter.save()
super(Product, self).save(*args, **kwargs)
我已经坚持了两个星期
更新
class Product(models.Model):
length = models.ForeignKey(Length, on_delete=models.SET_NULL, null=True, blank=True)
width = models.ForeignKey(Width, on_delete=models.SET_NULL, null=True, blank=True)
squaremeter = models.DecimalField(max_digits=6, decimal_places=4, blank=True)
UPDATE2:
class Length(models.Model):
value = models.PositiveSmallIntegerField(unique=True)
class Width(models.Model):
value = models.PositiveSmallIntegerField(unique=True)
答案 0 :(得分:0)
像这样编辑你的保存方法,
def save(self, *args, **kwargs):
self.squaremeter = self.length.value * self.width.value
super(Product, self).save(*args, **kwargs)