Shree Harsha S
Shree Harsha S

Reputation: 685

How to implement some action when data changes in sqlite?

I'm trying to implement some action in python (Django) whenever data changes in the sqlite db. There are different sources by which data enters.

As of now the plan is to keep monitoring the db on a thread for every 2 mins.

I would like to know if there are better ways to implement, some callback type whenever data changes in the db ?

Upvotes: 0

Views: 167

Answers (1)

v1k45
v1k45

Reputation: 8250

You can use Django's post_save signal for this job.

from django.db.models.signals import post_save
from django.dispatch import receiver

class MyModel(models.Model):
    # fields

# connect a receiver 
@receiver(post_save, sender=MyModel)
def do_some_action(sender, instance, **kwargs):
    do_that_action(instance)

Every time a MyModel object is saved, do_some_action function will be called.

See: Django docs about signals.

Upvotes: 2

Related Questions