Skip to content Skip to sidebar Skip to footer

Django Infer A Field From Other Fields

I am currently having a model: class Current(models.Model): field1 = models.IntegerField() field2 = models.IntegerField() field3 = models.IntegerField() I need t

Solution 1:

Something like this? But not sure why this would need to be saved in the database.

classCurrent(models.Model):  
    field1 = models.IntegerField()  
    field2 = models.IntegerField()
    field3 = models.IntegerField()  

    defsave(self, *args, **kwargs):
        self.field3 = self.field1 + self.field2
        super(Current, self).save(*args, **kwargs)

Solution 2:

You can overwrite the model's save() method. But why do you need to save field3 at all?

Post a Comment for "Django Infer A Field From Other Fields"