Python Django信号,在save listner上实现
我很难理解信号是如何工作的,我浏览了一些页面,但没有一页能帮助我理解 我有两个模型,我想创建一个信号,当记录保存在父模型中时,该信号将保存在子模型中。实际上,我希望该子对象在我的应用程序中侦听任何父对象,因为该子对象特别是通用外键 core/models.pyPython Django信号,在save listner上实现,python,django,Python,Django,我很难理解信号是如何工作的,我浏览了一些页面,但没有一页能帮助我理解 我有两个模型,我想创建一个信号,当记录保存在父模型中时,该信号将保存在子模型中。实际上,我希望该子对象在我的应用程序中侦听任何父对象,因为该子对象特别是通用外键 core/models.py from django.db import models from django.contrib.auth.models import User from django.contrib.contenttypes.models import
from django.db import models
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
class Audit(models.Model):
## TODO: Document
# Polymorphic model using generic relation through DJANGO content type
operation = models.CharField(max_length=40)
operation_at = models.DateTimeField("Operation At", auto_now_add=True)
operation_by = models.ForeignKey(User, db_column="operation_by", related_name="%(app_label)s_%(class)s_y+")
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
from django.db import models
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from core.models import Audit
class Instances(models.Model):
## TODO: Document
## TODO: Replace id with XXXX-XXXX-XXXX-XXXX
# Re
INSTANCE_STATUS = (
('I', 'In Progress' ),
('C', 'Cancelled' ),
('D', 'Deleted' ),
('P', 'Pending' ),
('O', 'Completed' )
)
id=models.CharField(max_length=200, primary_key=True)
status=models.CharField(max_length=1, choices=INSTANCE_STATUS, db_index=True)
audit_obj=generic.GenericRelation(Audit, editable=False, null=True, blank=True)
def save(self, *args, **kwargs):
# on new records generate a new uuid
if self.id is None or self.id.__len__() is 0:
import uuid
self.id=uuid.uuid4().__str__()
super(Instances, self).save(*args, **kwargs)
class Setup(models.Model):
## TODO: Document
# Polymorphic model using generic relation through DJANGO content type
content_type=models.ForeignKey(ContentType)
object_id=models.PositiveIntegerField()
content_object=generic.GenericForeignKey('content_type', 'object_id')
class Actions(models.Model):
ACTION_TYPE_CHOICES = (
('P', 'Python Script'),
('C', 'Class name'),
)
name=models.CharField(max_length=100)
action_type=models.CharField(max_length=1, choices=ACTION_TYPE_CHOICES)
class Tasks(models.Model):
name=models.CharField(max_length=100)
Instance=models.ForeignKey(Instances)
工作流/模型.py
from django.db import models
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
class Audit(models.Model):
## TODO: Document
# Polymorphic model using generic relation through DJANGO content type
operation = models.CharField(max_length=40)
operation_at = models.DateTimeField("Operation At", auto_now_add=True)
operation_by = models.ForeignKey(User, db_column="operation_by", related_name="%(app_label)s_%(class)s_y+")
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
from django.db import models
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.contrib.contenttypes import generic
from core.models import Audit
class Instances(models.Model):
## TODO: Document
## TODO: Replace id with XXXX-XXXX-XXXX-XXXX
# Re
INSTANCE_STATUS = (
('I', 'In Progress' ),
('C', 'Cancelled' ),
('D', 'Deleted' ),
('P', 'Pending' ),
('O', 'Completed' )
)
id=models.CharField(max_length=200, primary_key=True)
status=models.CharField(max_length=1, choices=INSTANCE_STATUS, db_index=True)
audit_obj=generic.GenericRelation(Audit, editable=False, null=True, blank=True)
def save(self, *args, **kwargs):
# on new records generate a new uuid
if self.id is None or self.id.__len__() is 0:
import uuid
self.id=uuid.uuid4().__str__()
super(Instances, self).save(*args, **kwargs)
class Setup(models.Model):
## TODO: Document
# Polymorphic model using generic relation through DJANGO content type
content_type=models.ForeignKey(ContentType)
object_id=models.PositiveIntegerField()
content_object=generic.GenericForeignKey('content_type', 'object_id')
class Actions(models.Model):
ACTION_TYPE_CHOICES = (
('P', 'Python Script'),
('C', 'Class name'),
)
name=models.CharField(max_length=100)
action_type=models.CharField(max_length=1, choices=ACTION_TYPE_CHOICES)
class Tasks(models.Model):
name=models.CharField(max_length=100)
Instance=models.ForeignKey(Instances)
我正在努力在审计模型中创建一个侦听器,这样我就可以将它与实例模型连接起来,如果在实例中插入一条新记录,它也会自动在审计中插入一条。然后,我计划将此侦听器连接到我应用程序中的几个模型
你知道我该怎么做吗?通过下面的代码,你可以用after\u save\u Instance\u handler方法连接实例对象的保存。在此方法中,您可以创建与审核的关系。另请参阅 我通常在models.py中添加信号,其中定义了发送方。不确定是否需要这样做
from django.db.models.signals import post_save
#####SIGNALS######
def after_save_instance_handler(sender, **kwargs):
#get the saved instance
instance_object = kwargs['instance']
#get the needed data
the_date = ...
the_user = ...
the_object_id = ...
#create the relation to the audit object
instance_object.audit_obj.create(operation="op963",operation_at=the_date,operation_by=the_user,object_id=the_object_id)
#connect the handler with the post save signal - django 1.1 + 1.2
post_save.connect(after_save_instance_handler, sender=Instances)
在django开发版本中,他们添加了一个装饰器来连接信号。
因此,您必须将此装饰程序添加到处理程序中,而不是上面的调用
@receiver(post_save, sender=Instances)
def after_save_instance_handler(sender, **kwargs):
使用下面的代码,您可以使用after_save_Instance_handler方法连接实例对象的保存。在此方法中,您可以创建与审核的关系。另请参阅 我通常在models.py中添加信号,其中定义了发送方。不确定是否需要这样做
from django.db.models.signals import post_save
#####SIGNALS######
def after_save_instance_handler(sender, **kwargs):
#get the saved instance
instance_object = kwargs['instance']
#get the needed data
the_date = ...
the_user = ...
the_object_id = ...
#create the relation to the audit object
instance_object.audit_obj.create(operation="op963",operation_at=the_date,operation_by=the_user,object_id=the_object_id)
#connect the handler with the post save signal - django 1.1 + 1.2
post_save.connect(after_save_instance_handler, sender=Instances)
在django开发版本中,他们添加了一个装饰器来连接信号。
因此,您必须将此装饰程序添加到处理程序中,而不是上面的调用
@receiver(post_save, sender=Instances)
def after_save_instance_handler(sender, **kwargs):
重要的是:为什么?由于子级对父级具有FK,因此子级的save可以简单地调用父级的save。你不需要信号。重要的是:为什么?由于子级对父级具有FK,因此子级的save可以简单地调用父级的save。你不需要信号。汤姆,你写的代码太棒了!我已经用一些型号试过了,效果非常好。尽管如此,我在使用具体的模型实例时遇到了一个问题。显然,因为我正在实例内部导入/使用Audit类,所以当我尝试在Audit内部导入实例时,Django会抛出一个错误。它无法看到我得到的类导入错误如下:无法导入名称实例不客气!那么您想在Audit中导入实例?这些模型是否放置在相同的models.py中?如果是,审计后是否定义了实例模型?如果是的话,我会想到一个想法。不确定是否导入,但至少通过设置ForeignKey,引用的模型必须放在引用模型之上。Tom,你编写的代码太棒了!我已经用一些型号试过了,效果非常好。尽管如此,我在使用具体的模型实例时遇到了一个问题。显然,因为我正在实例内部导入/使用Audit类,所以当我尝试在Audit内部导入实例时,Django会抛出一个错误。它无法看到我得到的类导入错误如下:无法导入名称实例不客气!那么您想在Audit中导入实例?这些模型是否放置在相同的models.py中?如果是,审计后是否定义了实例模型?如果是的话,我会想到一个想法。不确定是否导入,但至少通过设置ForeignKey,引用的模型必须放置在引用模型的上方。