I have tried in Models (with "def save") but give me error "int() argument must be a string, a bytes-like object or a number, not 'Length'".
I have fields "length" and "width" in foreignkey. And I want when submit a form to add value of "squaremeter" field to DB calculated based on the length and width selected
def save(self, *args, **kwargs):
self.squaremeter = self.length * self.width
self.squaremeter.save()
super(Product, self).save(*args, **kwargs)
For two weeks I'm stuck in this issue
Update:
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)
Edit your save method like this,
def save(self, *args, **kwargs):
self.squaremeter = self.length.value * self.width.value
super(Product, self).save(*args, **kwargs)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.