这是我保存模型的Django源码。
e = Element(title = title, description = des, location = city, date = timezone.now())
e.save()
这是我的mysql触发器。
DELIMITER $$
CREATE TRIGGER `ins_blog` AFTER INSERT ON `blog`
FOR EACH ROW BEGIN
INSERT INTO snippet
SET
id = LAST_INSERT_ID(`id`),
title = NEW.title,
snippet = NEW.description,
source_site = 'KJOBUS',
location = NEW.location,
date = NEW.date;
END$$
DELIMITER ;
没有触发器,它工作正常。我可以救 在mysql中,触发器工作正常,没有任何错误或警告。
但是,当我尝试通过Django保存数据时,会发出警告。
Exception Type: Warning
Exception Value:
Unsafe statement written to the binary log using statement format since BINLOG_FORMAT = STATEMENT. Statement is unsafe because it invokes a trigger or a stored function that inserts into an AUTO_INCREMENT column. Inserted values cannot be logged correctly.
Exception Location: /usr/lib/python2.7/dist-packages/MySQLdb/cursors.py in _warning_check, line 92
Python Executable: /usr/bin/python
哪里无法在django中使用触发器插入数据?
答案 0 :(得分:0)
您可以使用Django的post_save信号来替换您的mysql触发器。以下完全相同。
from django.signals import post_save
from django.dispatch import receiver
@reciever(post_save, sender=Element) # or sender=Blog, your example is unclear
def create_snippet(sender, instance, created, raw, **kwargs):
if created and not raw: # raw=True when loading from fixtures
Snippet.objects.create(
title=instance.title,
description=instance.description,
source_site='KJOBUS',
location=instance.description,
date=instance.date,
)
参考:https://docs.djangoproject.com/en/dev/ref/signals/#django.db.models.signals.post_save