0

I want to generate a push-notification whenever the database entry is updated if the current ID is 2 and a new tuple is added the id would be 3. so how can i be notified if that new id has been added to the database?

The data entry is done on the back-end by some python script no data has been added to the MySQL database by the user.

So every time a new tuple is added to the database I want my application to give me notification.

I am posting everything that might be relevant for your ease. please hep me with the code.

models.py creates table Offenses in the database models.py

class Offenses(models.Model):
    oid = models.IntegerField(primary_key=True)
    description = models.CharField(null=False, max_length=200)


    objects = UserManager()
    class Meta:
        db_table = "offenses"

from views.py i am adding entries to the database that has been retrieved from an API and stored in the database.here is the snippet of my views.py

response_body = json.loads(response.read().decode('utf-8'))
    for j in response_body:
        obj, created = Offenses.objects.get_or_create(oid=j['id'], defaults={'description': j['description'], 'assigned_to': j['assigned_to']})



    return render(request, 'table.html')
Hassan Ikram
  • 61
  • 1
  • 12

1 Answers1

0

You could do something like this:

class Offenses(models.Model):
    oid = models.IntegerField(primary_key=True)
    description = models.CharField(null=False, max_length=200)

    objects = UserManager() # Don't know why you use a user manager here.

    class Meta:
        db_table = "offenses"

    def save(self, force_insert=False, force_update=False, using=None,
             update_fields=None):  # Overriding the model save function.
        if self.id is None:  # A none id indicates that it is a new object.
            pass  # Send / store the notification.

        super().save(force_insert, force_update, using, update_fields)

How to do a push notification system it is another topic, but with this logic you can save the notification when the object is going to be created.

Hagyn
  • 922
  • 7
  • 14
  • where is that notification saved? – Hassan Ikram Jul 19 '19 at 07:24
  • My code do not create the notification, this is another different topic. There are some ways of doing it, but you could do a model Notification with a boolean seen and return the list of unseen notifications when you log-in in your page. – Hagyn Jul 19 '19 at 07:40
  • so this is the logic i have t use when there is a new entry in my db but it won't generate a notification. ok sir got it :) – Hassan Ikram Jul 19 '19 at 07:50