我使用 django-rest-framework 作为后端,使用 postgresql 作为数据库。原始 SQL 脚本可能会更改数据库,我希望在发生这些更改时在后端收到通知,以便我可以将更改通知不同的用户。
我已经检查过类似 https://gist.github.com/pkese/2790749 的帖子用于在 python 和一些 SQL 脚本中接收通知
CREATE TRIGGER rec_notify_trig AFTER INSERT OR UPDATE OR DELETE ON rec
FOR EACH ROW EXECUTE PROCEDURE rec_notify_func()
我的问题是我不知道如何在 django-rest-framework 中将它们连接在一起,比如我应该将 SQL 脚本放在哪里,将 python 设置放在哪里以便我可以将它们连接在一起。任何建议将不胜感激。
最佳答案
我会在 djangorestframework 端创建一个端点来接受通知。
然后,在您的 rec_notify_func()
中,您可以调出并点击您的端点,您可以在其中执行任何必要的最终用户通知。
CREATE EXTENSION plpython3u;
CREATE FUNCTION rec_notify_func(notification_endpoint_uri text) RETURNS text AS $$
from urllib.request import urlopen
data = urlopen(notification_endpoint_uri)
return data.read()
$$ LANGUAGE plpython3u;
NOTE: You need to have plpython installed on the system in order to enable the extension.
On ubuntu something like this:
sudo apt-get install postgresql-plpython3-9.6
关于django - Postgresql 由 sql 脚本更新,在后端得到通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57085634/