Skip to content

Instantly share code, notes, and snippets.

@thorin-schiffer
Created December 31, 2021 15:27
Show Gist options
  • Save thorin-schiffer/c11abf6649a9703f960596bc70bdccf6 to your computer and use it in GitHub Desktop.
Save thorin-schiffer/c11abf6649a9703f960596bc70bdccf6 to your computer and use it in GitHub Desktop.
Django-plpy bulk operations
from django_plpy.installer import pltrigger
from tests.books.models import Book
@pltrigger(event="UPDATE", 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
Book.objects.values('amount_stock')
# <QuerySet [{'amount_stock': 30}, {'amount_stock': 30}, {'amount_stock': 30}]>
Book.objects.all().update(name="test")
# 3
Book.objects.values('amount_stock')
# <QuerySet [{'amount_stock': 40}, {'amount_stock': 40}, {'amount_stock': 40}]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment