Search code examples
pythondjangodjango-users

Django: Deleting a User Profile


If I have the following code to create a user profile, do I have to delete it when I delete the user from the db. If so, how?

def create_user_info(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)

post_save.connect(create_user_profile, sender=User)

Solution

  • Assuming that you have a OneToOneField on your UserProfile model which points to User, then you do not have to delete the profile explicitely.

    In that case, Django's delete cascade will automatically follow the relationship backwards from the User to the Profile and delete the profile object for you.