I want to save the django model with computed field so that I can apply search on that.
class TestModel(models.Model):
x = models.CharField(max_length=16)
z = models.CharField(max_length=16)
# I want a field like below and also saves in databse
# computed = computed()
def computed(self):
result = self.x + self.y
return result
class TestModel(models.Model):
x = models.CharField(max_length=16)
z = models.CharField(max_length=16)
computed = models.CharField(max_length=32, editable=False)
def save(self, *args, **kwargs):
self.computed = self.x + self.y
super(TestModel, self).save(*args, **kwargs)
Here is what editable
option does. More.
We should override save() method.
class TestModel(models.Model):
x = models.CharField(max_length=16)
z = models.CharField(max_length=16)
computed = models.CharField(max_length=32)
def get_computed(self):
result = self.x + self.y
return result
def save(self, *args, **kwargs):
self.computed = self.get_computed()
super(TestModel, self).save(*args, **kwargs)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With