Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete user when deleting UserProfile

Tags:

python

django

I created UserProfile (extends from User) and written to settings.py

 AUTH_PROFILE_MODULE = 'mainapp.UserProfile'.

When I delete UserProfile (from admin area) I would delete also User item.

I try delete user so self.user.delete(), but method delete (in UserProfile) don't call. Why ?

This is my code:

class UserProfile(models.Model):
    avatar = models.ImageField(upload_to = settings.PATH_AVATARS, blank=True)
    url = models.URLField(blank=True)
    user = models.OneToOneField(User)

    def __unicode__(self):
        return self.user.username

    def delete(self, *args, **kwargs):
        self.user.delete()
        super(UserProfile, self).delete(*args, **kwargs)
like image 683
yAnTar Avatar asked Feb 20 '12 14:02

yAnTar


People also ask

Does deleting a user profile delete the account?

Once you do this, their account should be deleted. Quick tip: Deleting a user from your Windows 10 machine will permanently delete all of their associated data, documents, and more. If needed, ensure the user has a backup of any important files they want to keep before you delete.

Does deleting a user delete all data?

Before you delete or remove a userAll of a user's data is deleted, unless you transfer it to another user. You might need to transfer some data, such as Gmail data or Drive files, before you delete the user. Some data isn't deleted, such as any groups the user created.

How do I get rid of another user on my profile?

Select Start > Settings > Accounts > Other users. Select the person's name or email address, then select Remove.

Does deleting a Windows user delete files?

Yes the files of that user will be removed however you are given an option by Windows to save a copy of the files onto your main user profile.


1 Answers

First, to answer why "delete()" is not called from the admin. This statement is:

  1. True in the case of deleting objects from the list view, ie. /admin/auth/user/ check some boxes then click Actions -> delete), this is because the delete() method of a queryset is called,
  2. Wrong in the case of deleting an object from the change_form, ie. /admin/auth/user/1/ click on delete, this is where the delete() method of the object is called

That said, _delete signals are well supported. Here is how you can use it:

from django.db.models import signals

def delete_user(sender, instance=None, **kwargs):
    try:
        instance.user
    except User.DoesNotExist:
        pass
    else:
        instance.user.delete()
signals.post_delete.connect(delete_user, sender=UserProfile)

This is how i tested it:

In [1]: from django.contrib.auth.models import User; from testapp.models import UserProfile; User.objects.all().delete(); UserProfile.objects.all().delete()

In [2]: user=User(username='foo'); user.save()

In [3]: profile=UserProfile(user=user); profile.save()

In [4]: UserProfile.objects.all().delete()

In [5]: User.objects.all()
Out[5]: []

Of course, this also works when the delete() method of the object is called:

In [1]: from django.contrib.auth.models import User; from testapp.models import UserProfile; User.objects.all().delete(); UserProfile.objects.all().delete()

In [2]: user=User(username='foo'); user.save()

In [3]: profile=UserProfile(user=user); profile.save()

In [4]: profile.delete()

In [5]: User.objects.all()
Out[5]: []

Note that because of cascade delete, this works both ways:

In [1]: from django.contrib.auth.models import User; from testapp.models import UserProfile; User.objects.all().delete(); UserProfile.objects.all().delete()

In [2]: user=User(username='foo'); user.save()

In [3]: profile=UserProfile(user=user); profile.save()

In [4]: user.delete()

In [5]: User.objects.all()
Out[5]: []

In [6]: UserProfile.objects.all()
Out[6]: []

If you need to know more about signals, refer to Django's extensive documentation.

like image 75
jpic Avatar answered Sep 20 '22 23:09

jpic