Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i create Generated/Computed column Postgres/DJANGO?

How can I create Generated/Computed column Postgres/DJANGO?

I tried in both ways:

(1) By a Class:

class Product(models.Model):
    name = models.CharField(max_length=200, null=True)
    precio_costo = models.FloatField(null=True)
    cantidad = models.IntegerField(null=True)
    monto_stock = models.FloatField(
        always_generated='precio_costo * cantidad', stored=True)

ERROR I GET:

TypeError: init() got an unexpected keyword argument 'always_generated'

(2) Directly by POSTGRES Admin

But I can't update or add a new field, Default value is required by Django.

like image 578
Isesy Miguel Ng Avatar asked Feb 04 '26 21:02

Isesy Miguel Ng


1 Answers

In Django >5.0 you can use GeneratedField.

like image 197
simplylizz Avatar answered Feb 07 '26 10:02

simplylizz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!