Home > Software design >  Notification in django rest API
Notification in django rest API

Time:06-24

I want to make a notice in django that when you add a new entry to the database the admin I do everything in the Django Rest API

CodePudding user response:

It depends on how you want the admin to be contacted. I'm going to assume you already have a model, a view and a serializer (preferably a ModelSerializer) inside your Django app. If not, take a look over the links I've sent to you.

At this moment, I'm thinking about contacting the admin of the system in two ways: 1.) by sending an email to the administrator of the system or 2.) by creating an object of type "Notification" (or named hovewer you want), which the admin can inspect from the panel.

For the first option, you can just override one of the REST serializer's methods (preferably create()). create() is the actual method that gets called after you specify the fields for your model before sending a POST request to create it. You can add some send_email() method inside your create(), containing some of the validated data. send_email() is also mentioned inside REST's documentation (exactly at the Serializing section), but I'm not sure it helps that much. You can check up its proper syntax by just googling it.

The second way would be by just creating a new model (called Notification? maybe) and add a new entry inside the database, everytime a request of object creation is made (so you'll basically still need to override the create() method inside your serializer). You can also add a field (boolean? maybe) called isRead, which the admin can mark as true/false. If any notifications with isRead == false are found, they'll appear on the admin's page.

CodePudding user response:

def create_profile(sender, instance, created, **kwargs):
    if created:
        instance.rooms.room_bool = instance.room_bool
        instance.rooms.save()
        token = '5419456477:AAHuyagslasglfsE9O-90vgiDHVTiV2Kmq8JWXiXFRNw'
        URL = 'https://api.telegram.org/bot'   token   '/sendMessage'
        for chat_id in ADMINS:
            try:
                data = {'chat_id': chat_id, 'text': "Забронирован один номер через Ресепшен\n\n"
                                                    "Посмотрите по ссылке http://127.0.0.1:8000/admin/reg_admin/registration/\n\n"
                                                    "Нажмите /start чтобы вывести меню администратора"}
                requests.post(URL, data=data)
            except Exception:
                pass

using signals.py, I sent a message to the bot via json

  • Related