am having difficulties understanding how signals works, I went through some pages but none of them helped me get the picture.
I have two models, I would like to create a signal that will save in the child model when a record is saved in the parent. Actually, I want the child to be listening across my application for any parent since this child in particular of a generic foreign key.
core/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')
workflow/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)
Am struggling to create a listener in Audit model so I can connect it with Instance model, If a new record inserted in Instance it will automatically inserts one in Audit as well. Then, am planning to connect this listener to several models in my app,
Any idea how I can do such a thing?