from django.db import models # Create your models here. class Role(models.Model): name = models.CharField(max_length=50) def __unicode__(self): return self.name def save(self, *args, **kwargs): is_new = self.id is None super(Role, self).save(*args, **kwargs) if is_new: for service in Service.objects.all(): alert = Alert(role=self, service=service, enable=False) alert.save() class Service(models.Model): name = models.CharField(max_length=50) def __unicode__(self): return self.name def save(self, *args, **kwargs): is_new = self.id is None super(Service, self).save(*args, **kwargs) if is_new: for role in Role.objects.all(): alert = Alert(role=role, service=self, enable=False) alert.save() class Alert(models.Model): enable = models.BooleanField() role = models.ForeignKey(Role) service = models.ForeignKey(Service) class Meta: unique_together = (('role', 'service'))