Django code or MySQL triggers(Django 代码或 MySQL 触发器)
问题描述
我正在使用 Django 制作一个使用 MySQL 数据库的网络服务.客户端通过由 Django 处理的 URL 与我们的数据库进行交互.现在我正在尝试创建一种行为,当某个表被修改时自动进行一些检查/记录,这自然意味着 MySQL 触发器.但是,我也可以在 Django 中执行此操作,在执行表修改的请求处理程序中.我认为 Django 还没有触发器支持,所以我不确定哪个更好,通过 Django 代码或 MySQL 触发器来做.
I'm making a web service with Django that uses MySQL database. Clients interface with our database through URLs, handled by Django. Right now I'm trying to create a behavior that automatically does some checking/logging whenever a certain table is modified, which naturally means MySQL triggers. However I can also do this in Django, in the request handler that does the table modification. I don't think Django has trigger support yet, so I'm not sure which is better, doing through Django code or MySQL trigger.
任何了解这些选项的性能的人都愿意透露一些信息吗?提前致谢!
Anybody with knowledge on the performance of these options care to shed some light? Thanks in advance!
推荐答案
解决你所描述的问题的方法有很多:
There are a lot of ways to solve the problem you've described:
- 应用逻辑
- 特定于视图的逻辑 -- 如果行为特定于单个视图,则将更改放入视图中.
- 特定于模型的逻辑 -- 如果行为特定于单个模型,则 覆盖模型的 save() 方法.
- Application Logic
- View-specific logic -- If the behavior is specific to a single view, then put the changes in the view.
- Model-specific logic -- If the behavior is specific to a single model, then override the save() method for the model.
就我个人而言,我更喜欢使用覆盖 save() 方法或使用 Django 信号.使用特定于视图的逻辑可以让您在具有相同模型的多个视图的大型应用程序中脱颖而出.
Personally, I prefer using either overriding the save() method, or using a Django signal. Using view-specific logic can catch you out on large applications with multiple views of the same model(s).
这篇关于Django 代码或 MySQL 触发器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Django 代码或 MySQL 触发器


基础教程推荐
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01