Skip to content

Instantly share code, notes, and snippets.

@Ilgrim Ilgrim/__init__.py forked from kylefox/__init__.py
Created Jun 10, 2019

Embed
What would you like to do?
Connecting Django Signals using the AppConfig ready handler: https://docs.djangoproject.com/en/1.10/ref/applications/#django.apps.AppConfig.ready
default_app_config = 'cn.apps.users.config.UsersAppConfig'
from django.apps import AppConfig
class UsersAppConfig(AppConfig):
name = 'cn.apps.users'
def ready(self):
import signals # noqa
from django.dispatch import receiver
from django.db.models.signals import post_save, pre_delete
from django.contrib.auth.models import User
from .models import UserProfile
@receiver(post_save, sender=User, dispatch_uid="users.create_user_profile")
def create_user_profile(**kwargs):
"""
Automatically create a UserProfile for any newly created User.
"""
user = kwargs.get('instance')
if not hasattr(user, 'profile'):
UserProfile.objects.create(user=user)
@receiver(pre_delete, sender=UserProfile,
dispatch_uid="profiles.delete_profile_image")
def delete_profile_image(**kwargs):
"""
Deletes the UserProfile.image file.
"""
kwargs.get('instance').image.delete(save=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.