Django发出信号,在save监听器上实现

时间:2010-12-31 11:34:33

标签: python django

我很难理解信号是如何工作的,我浏览了一些页面,但没有一个帮助我了解信号。

我有两个模型,我想创建一个信号,当父记录中保存记录时,该信号将保存在子模型中。实际上,我希望孩子在我的应用程序中监听任何父母,因为这个孩子特别是普通外键。

芯/ models.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')

工作流/ models.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
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)

我很难在Audit模型中创建一个监听器,因此我可以将它与Instance模型连接,如果在Instance中插入一条新记录,它也会自动在Audit中插入一条。然后,我打算将这个监听器连接到我的应用程序中的几个模型,

知道我怎么能这样做吗?

1 个答案:

答案 0 :(得分:4)

使用下面的代码,您可以使用after_save_instance_handler方法连接Instance对象的保存。在此方法中,您将创建与Audit的关系。另请参阅generic relations doc

我通常在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 1.2 signals

在django开发版中,他们添加了一个装饰器来连接信号。 因此,不是上面的调用,你必须将这个装饰器添加到处理程序

@receiver(post_save, sender=Instances)
def after_save_instance_handler(sender, **kwargs):

django dev signals