Django how to delete user's profile and posts and all assocation after user deleted?
Asked Answered
Y

3

9

I'm writing a django project. And want to know after user deletes his own account, is there a way django build-in to auto delete all object related to this user(e.g. some generic foreign_key)? Or I should use signal "post_delete" to delete every objects related?

Yee answered 25/1, 2012 at 22:51 Comment(2)
Are you sure the related objects aren't already being deleted? Non-nullable ForeignKeys must be deleted to avoid an IntegrityError, and IIRC Django will do so by default. By generic foreign key, do you mean GenericForeignKey specifically? Because that gets more complicated.Leyba
@Leyba I think I didn't mean GenericForeignKey specifically here. What I mean is the ON DELETE CASCADE which I already got an answer. I have not understood clearly what GenericForeignKey does as long as Contenttype, I'm studying. Another question which maybe not relative to this one, but I want to ask is, I found some user profile implementation, they all use ForeignKey instead of OneToOne. Are they suppose a many-to-one relation here or it doesn't matter.Yee
O
14

When Django deletes an object, by default it emulates the behavior of the SQL constraint ON DELETE CASCADE -- in other words, any objects which had foreign keys pointing at the object to be deleted will be deleted along with it.

https://docs.djangoproject.com/en/dev/topics/db/queries/#deleting-objects

b = Blog.objects.get(pk=1)
# This will delete the Blog and all of its Entry objects.
b.delete()
Omura answered 25/1, 2012 at 23:1 Comment(0)
H
7

Django recommends not deleting users since foreign keys will break. It's for this reason that they included the is_active method.

See https://docs.djangoproject.com/en/1.3/topics/auth/#django.contrib.auth.models.User.is_active

Hexagonal answered 25/1, 2012 at 23:16 Comment(1)
This is an old and possibly outdated recommendation and the link is broken.Pubes
O
5

You should explicitly delete all of the generic foreign key references to the original object before you delete the original object. For example

Image.objects.filter( object_id=object_to_be_deleted.id,content_type = ContentType.objects.get_for_model(bject_to_be_deleted.get_profile() )).delete()
object_to_be_deleted.delete()

The cascading delete is great when it works, for example, for one-to-one relationships in the models, but it doesn't seem to work for generic foreign key relationships.

Opalopalesce answered 25/1, 2012 at 23:3 Comment(3)
Although I agree with your recommendation, the statement "it doesn't seem to work for generic foreign key relationships" needs further explanation as this is not the usual case.Pubes
@Pubes This was true in 2012 (Django 1.4?) but it appears from the documentation ( docs.djangoproject.com/en/1.8/ref/contrib/contenttypes/… ) that now "if you delete an object that has a GenericRelation, any objects which have a GenericForeignKey pointing at it will be deleted as well" which may have been a Django 1.7 change.Opalopalesce
@MarkChackerian I see, thanks for the info and for the answer.Pubes

© 2022 - 2024 — McMap. All rights reserved.