English 中文(简体)
Django信号,在救助名单上执行
原标题:Django signals, implementing on save listner

我难以理解信号是如何发挥作用的,但我走过了几页,但都没有帮助我了解情况。

我有两种模式,我谨发出信号,在父母保存记录时,将挽救儿童模式。 实际上,我要求儿童听取我向任何父母提出的申请,因为这一儿童,特别是一般的外国钥匙。

核心/模型。

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)

试图创建审计模式的听众,使我能够将其与事例模式联系起来,如果新记录在实例中插入,也会自动在审计中插入一个记录。 然后,我计划把这名听众与我的记忆中的若干模式联系起来。

任何想法,我如何能够做这样的事情?

最佳回答
问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签