Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
django-pgtrigger
helps you write Postgres triggers for your Django models.
Triggers can solve a variety of complex problems more reliably, performantly, and succinctly than application code. For example,
pgtrigger.Protect
).pgtrigger.ReadOnly
).pgtrigger.SoftDelete
).pgtrigger.FSM
).pgtrigger.UpdateSearchVector
).User.objects.create_user
and not User.objects.create
).All of these examples require no overridden methods, no base models, and no signal handling.
Install django-pgtrigger
with pip3 install django-pgtrigger
and add pgtrigger
to settings.INSTALLED_APPS
.
pgtrigger.Trigger
objects are added to triggers
in model Meta
. django-pgtrigger
comes with several trigger classes, such as pgtrigger.Protect
. In the following, we're protecting the model from being deleted:
import pgtrigger
class ProtectedModel(models.Model):
"""This model cannot be deleted!"""
class Meta:
triggers = [
pgtrigger.Protect(name="protect_deletes", operation=pgtrigger.Delete)
]
When migrations are created and executed, ProtectedModel
will raise an exception anytime a deletion is attempted.
Let's extend this example further and only protect deletions on inactive objects. In this example, the trigger conditionally runs when the row being deleted (the OLD
row in trigger terminology) is still active:
import pgtrigger
class ProtectedModel(models.Model):
"""Active object cannot be deleted!"""
is_active = models.BooleanField(default=True)
class Meta:
triggers = [
pgtrigger.Protect(
name="protect_deletes",
operation=pgtrigger.Delete,
condition=pgtrigger.Q(old__is_active=True)
)
]
django-pgtrigger
uses pgtrigger.Q
and pgtrigger.F
objects to conditionally execute triggers based on the OLD
and NEW
rows. Combining these Django idioms with pgtrigger.Trigger
objects can solve a wide variety of problems without ever writing SQL. Users, however, can still use raw SQL for complex cases.
Triggers are installed like other database objects. Run python manage.py makemigrations
and python manage.py migrate
to install triggers.
If triggers are new to you, don't worry. The pgtrigger docs cover triggers in more detail and provide many examples.
django-pgtrigger
is compatible with Python 3.8 - 3.12, Django 4.2 - 5.1, Psycopg 2 - 3, and Postgres 13 - 16.
View the django-pgtrigger docs here to learn more about:
Install django-pgtrigger
with:
pip3 install django-pgtrigger
After this, add pgtrigger
to the INSTALLED_APPS
setting of your Django project.
After you've read the docs, check out this tutorial with interactive examples from a Django meetup talk.
The DjangoCon 2021 talk also breaks down triggers and shows several examples.
For information on setting up django-pgtrigger for development and contributing changes, view CONTRIBUTING.md.
FAQs
Postgres trigger support integrated with Django models.
We found that django-pgtrigger demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.