django-safedelete icon indicating copy to clipboard operation
django-safedelete copied to clipboard

model.objects.all() returns deleted objects

Open rlwy opened this issue 5 years ago • 8 comments

Was wondering if anyone else is having this issue where .all() is returning deleted objects as well. To bypass for now i have to query for .exclude(deleted__isnull=False) on my model objects.

rlwy avatar May 09 '19 02:05 rlwy

Could you provide some more information?

AndreasBackx avatar May 10 '19 22:05 AndreasBackx

Hi @AndreasBackx, in my project I have an user model which I extended from Django's default user model. In my model, I implement SOFT_DELETE_CASCADE. When I delete a user using User.delete(), the deleted column in the user table is replaced with the datetime of when the user was deleted. However, in my view file, when I query User.objects.all() the deleted user still gets returned along with all the other objects.

rlwy avatar May 12 '19 23:05 rlwy

Did you overwrite the manager of your model without inheriting from SafeDeleteManager?

Gagaro avatar May 13 '19 09:05 Gagaro

This is what I have in my user.py model and view files: https://gist.github.com/rlwy/b7444d890bc9aa8ad90876b5388ae876

rlwy avatar May 13 '19 18:05 rlwy

AbstractUser has a custom manager. You should create your own manager which inherits from both UserManager and SafeDeleteManager and set it on your model.

Gagaro avatar May 14 '19 07:05 Gagaro

Hello @Gagaro. I'm getting the same error, Could you please help me with this.

I have the same model as rlwy

Singh-Sg avatar Feb 24 '20 11:02 Singh-Sg

I can use this to override but I'm not pretty sure,

class CustomManager(models.Manager):
    def get_queryset(self):
        return super(CustomManager, self).get_queryset().filter(delete=None)

class User(models.Model):
    # Blah blah
    objects = CustomManager()

I want to implement safedelete package in my project.

Singh-Sg avatar Feb 24 '20 12:02 Singh-Sg

Your manager needs to inherits from both UserManager (from django.contrib.auth) and SafeDeleteManager.

Gagaro avatar Mar 02 '20 09:03 Gagaro