Deleting a user model that extends abstractuser
I am trying to delete a user object that belongs to a User class that extends AbstractUser. The extending class looks something like this:
class User(AbstractUser): name = CharField("Name", blank=True, max_length=255) def __str__(self): return self.username
When I try to delete a user like this:
user = User.objects.get(pk=self.user_id) user.delete()
The user object is not deleted (at least from checking the user table in the database). Other models that have
on_delete=models.CASCADE to my custom user class are deleted as expected but the actual user object is NOT deleted.
When I try
user = User.objects.get(pk=self.user_id) user.delete() user.delete() # adding extra delete()
i.e. an extra
user.delete(), then the user object is deleted, but also raises an exception (which is not unexpected)
User object can't be deleted because its id attribute is set to None.
but the user object along with other objects with
FK relationship to the User class are deleted (as expected). Sounds like the object was "deleted" the first time I called
user.delete()? But I can still see the row in my user's table.
Not sure if related but, I see that the type of User to delete is
<class 'django.utils.functional.SimpleLazyObject'> not the
User model class.
So, I'm trying to delete a user object that extends abstractuser class but I'm having to call
delete() twice on the object, which is not normal (I think), how can I get rid of this?