Re: deleting a user model that extends abstractuser

2023-02-03 Thread Mike Dewhirst

On 4/02/2023 7:58 am, d07 wrote:


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.




Here's a guess ...

The models.CASCADE seems to be working and everything seems fine. So 
when does the vital object at the head of the cascade get deleted?


It has to be when the delete transaction gets committed to the database 
and all the loose ends are cleaned up.


Therefore, some loose end somewhere is preventing transaction completion.

How clean is your User model?


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.



That says the database needs to clean up some loose ends for which it 
needs the id of the User being deleted.


Does the User model have a foreign key with on_delete = models.SET_NULL

Transactions need to be committed successfully or they roll back.

Good luck

M

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  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?


AbstarctUser is from from django.contrib.auth.models import AbstractUser

--
You received this message because you are subscribed to the Google 
Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send 
an email to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/2f4ffda8-2eac-4546-a8fa-276bfb38df8fn%40googlegroups.com 
.



--
Signed email is an absolute defence against phishing. This email has
been signed with my private key. If you import my public key you can
automatically decrypt my signature and be sure it came from me. Just
ask and I'll send it to you. Your email software can handle signing.

--
You received this message because you are subscribed to the Google Groups "Django 
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/cd703c26-1a23-56e2-0a6c-30bcdefa186d%40dewhirst.com.au.


OpenPGP_signature
Description: OpenPGP digital signature


deleting a user model that extends abstractuser

2023-02-03 Thread d07


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  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?

AbstarctUser is from from django.contrib.auth.models import AbstractUser

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/2f4ffda8-2eac-4546-a8fa-276bfb38df8fn%40googlegroups.com.