Alexis
Alexis

Reputation: 25163

Updating when database has changed in Django

I have many records in my database that are displayed on a screen. However, the records are user generated and sometimes have to be removed. Each record has a show field which is initially always set to true. When we get content that has to be removed a human will set it to false in the Django admin interface. When this happens, we need the bad content to be removed from the screen. So my question is, in the Django interface, what is the way to tell when a record has been updated and do something in response to this change?

Upvotes: 0

Views: 562

Answers (1)

Guilherme David da Costa
Guilherme David da Costa

Reputation: 2368

You should read about signals:

An Idea on how doing this:

from django.core.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=MyModel)
def my_handler(sender, instance, created, raw, **kwargs):
    if created: # True for save, False for update
        ...

I think this could help you tell when the record its being updated and when you can do something about it.

But if the user seeing the records wont have to refresh the page so the record is hidden, then you could use websockets to receive that information your signal sent. Or you can just do ajax requests every 20-30 seconds to check all the records and discovery which one is hidden, or you can check a list of latest hidden records that your signal will populate.

Anyway, there is different ways of doing this.

Upvotes: 2

Related Questions