Skip to content
Advertisement

How can I delete all user data when deleting profile on Django using signals.py

how can I delete all user data when deleting a user profile on Django using signals.py? I am trying to delete a custom user model using Django and when the profile is deleted I want to delete all data of that user using signals.py how can I create it?

@receiver(post_delete, sender=models.Profile_user)
def delete_auth_user_model_handler(sender, instance, *args, **kwargs):
    user_instance = instance.user
    user_instance.delete()

I want to delete blogs of the user-written when the profile is deleted

Advertisement

Answer

You could just declare your models as ForeignKeys to your user. This way when you delete the user , everything associated with it will be deleted:

class Blogs(models.Model):
author= models.ForeignKey(User, on_delete=models.CASCADE, blank=True, null=True, default=None)
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement