'In Django : How can I multiply two columns and save to another column in update query?

class A(models.Model):
    revenue = models.FloatField()
    list_price = models.FloatField()
    unit_sold = models.FloatField()

I have this model which has 1M records in list_price and unit_sold I want to fill revenue column using list_price * unit_sold



Solution 1:[1]

In overwrite your save function in the form, add result = list_price * unit_sold before save.

    def save(self, commit=True):
        A = super(AForm, self).save(commit=False)
        revenue = list_price * unit_sold

        if commit:
            A.save()
        return A

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1