Core components of the pgtrigger module
mainThe pgtrigger module provides a declarative API for defining PostgreSQL triggers within Django. It is organized into several functional clauses that map to SQL trigger syntax:
Level
Defines the granularity of the trigger:
Row: Executes for each row affected.Statement: Executes once per SQL statement.
When (Timing)
Defines when the trigger fires relative to the operation:
After: Fires after the operation.Before: Fires before the operation.InsteadOf: Fires instead of the operation (typically for views).
Operation
Defines which SQL operations trigger the execution:
Insert,Update,Delete,Truncate,UpdateOf.
Referencing
Referencing: Used to access theNEWandOLDrow data.
Timing (Execution Mode)
Immediate: Executes immediately.Deferred: Executes at the end of the transaction.
Func
Func: Allows calling PostgreSQL functions.
Conditions
Logic used to determine if a trigger should fire, including:
Condition,AnyChange,AnyDontChange,AllChange,AllDontChange.- Django-like lookups:
Q,F,IsDistinctFrom,IsNotDistinctFrom.
Triggers
High-level trigger abstractions:
Trigger: The base class for custom triggers.Protect,ReadOnly,SoftDelete,FSM,UpdateSearchVector,Composer.
Runtime & Registry
constraints,ignore,is_ignored,schema: Manage runtime behavior and constraints.register,registered: Manage the internal registry of triggers.
Installation & Management
install,uninstall: Handle database setup/teardown.enable,disable: Toggle trigger state.prunable,prune: Manage cleanup of unused triggers.