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)
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.
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.
Select Start > Settings > Accounts > Other users. Select the person's name or email address, then select Remove.
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.
First, to answer why "delete()" is not called from the admin. This statement is:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With