Created
December 31, 2021 15:23
-
-
Save thorin-schiffer/10e0e23c70ebdd296e04558bba0e4054 to your computer and use it in GitHub Desktop.
Django-plpy triggers with ORM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django_plpy.installer import pltrigger | |
from django.db.models import Model, CharField, IntegerField | |
class Book(Model): | |
name = CharField(max_length=10) | |
amount_stock = IntegerField(default=20) | |
amount_sold = IntegerField(default=10) | |
@pltrigger(event="INSERT", when="BEFORE", model=Book) | |
def pl_update_amount(new: Book, old: Book, td, plpy): | |
# don't use save method here, it will kill the database because of recursion | |
new.amount_stock += 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment