Skip to content

Instantly share code, notes, and snippets.

@artschwagerb
Last active December 29, 2015 20:39
Show Gist options
  • Save artschwagerb/7725254 to your computer and use it in GitHub Desktop.
Save artschwagerb/7725254 to your computer and use it in GitHub Desktop.
Django Signals
from django.db import models
from django.contrib.auth.models import User
class Person(models.Model):
employee_id = models.IntegerField(unique=True,editable=False)
username = models.CharField(max_length=50,unique=True,editable=False)
state_id = models.IntegerField(unique=True,null=True,blank=True,editable=False)
email = models.EmailField(max_length=50,unique=True,editable=False)
firstname = models.CharField(max_length=50,editable=False)
lastname = models.CharField(max_length=50,editable=False)
telephone = models.CharField(max_length=25,editable=False)
date_added = models.DateTimeField('date_added',auto_now_add=True, editable=False)
date_updated = models.DateTimeField('date_updated',auto_now_add=True, auto_now=True, editable=False)
django_user = models.ForeignKey(User,null=True,blank=True)
def __unicode__(self):
return u'%s' % (self.username)
from django.db import models
from personnel.models import *
def user_post_delete(sender, instance, **kwargs):
try:
person_item = Person.objects.get(username = instance.username)
person_item.django_user = None
person_item.save()
except:
pass
def user_post_save(sender, instance, **kwargs):
try:
person_item = Person.objects.get(username = instance.username)
person_item.django_user = instance
person_item.save()
except:
pass
models.signals.post_delete.connect(user_post_delete, sender=User)
models.signals.post_save.connect(user_post_save, sender=User)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment