Skip to content

Instantly share code, notes, and snippets.

@thorin-schiffer
Created December 31, 2021 15:23
Show Gist options
  • Save thorin-schiffer/10e0e23c70ebdd296e04558bba0e4054 to your computer and use it in GitHub Desktop.
Save thorin-schiffer/10e0e23c70ebdd296e04558bba0e4054 to your computer and use it in GitHub Desktop.
Django-plpy triggers with ORM
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