Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] post_deleted listener raise an Exception on user delete Cascade #162

Closed
DylannCordel opened this issue Mar 18, 2015 · 1 comment
Closed

Comments

@DylannCordel
Copy link
Contributor

Hi,

When we delete an user, cascade delete will try to delete posts and profile too. When post are deleted, post_delete signal is sent and post_deleted callback is called. This callback update the post_count of the author's profile. Do do that, it call util.get_pybb_profile(instance.user) and here is the problem :

With the default Profile which has an AutoOneToOneField relation with the user, the profile is re-created. So, there is not any error, but once the user and all its posts are deleted, the profile still exists.

With a custom Profile wich use a classic OneToOneField, a DoesNotExists exception is raised because profile has already be deleted.

This is an other good point for #54 : use custom signals instead of default models signals : post_deleted callback should not be called when we batch delete posts (like cascade or via api for eg.). Furthermore, if I delete 1 000 posts from one user, his post_count will be recalculated 1 000 times (so 2 000 extra SQL queries will be performed instead of one)

@DylannCordel
Copy link
Contributor Author

I just create a pull request with a temporary patch and tests to check this error does not exist anymore.

For users which have the same problem, you can also ugly patch you own code :

#your models.py

from django.contrib.auth import get_user_model
User = get_user_model()

def user_delete(self, *args, **kwargs):
    from pybb.models import Post
    Post.objects.filter(user=self).delete()
    super(User, self).delete(*args, **kwargs)

delete on queryset "only" perform a SQL Delete (does not call delete method for each instances, so post_deleted is never called).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant