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

How can I use SafeDeleteQueryset and SafeDeleteManager at the same time? #239

Open
neoneo40 opened this issue Apr 7, 2024 · 0 comments
Open

Comments

@neoneo40
Copy link

neoneo40 commented Apr 7, 2024

# models.py

class BoardQuerySet(SafeDeleteQueryset):

    def is_public(self):
        return self.filter(is_public=True)

    def for_user(self, user):
        return self.filter(user=user)


class BoardManager(SafeDeleteManager):
    def get_queryset(self):
        return BoardQuerySet(self.model, self._db).select_related('user', 'board_category')

    def is_public(self):
        return self.get_queryset().is_public()

    def for_user(self, user):
        return self.get_queryset().for_user(user)
        
        
class Board(BaseBoard):
    pass

    objects = BoardManager()
In [4]: Board.deleted_objects.all().count()
Out[4]: 5

In [6]: Board.all_objects.all().count()
Out[6]: 6

In [8]: Board.objects.all().count()
Out[8]: 6

I don't understand why Board.objects.all().count() gets 6 instead of 1.
When you apply objects = SafeDeleteManager(), 1 is normally displayed.

The reason I did that is to call the function in a chainable manner.
Board.objects.is_public().for_user(user=user)
It's meant to be done this way, but it doesn't work.

Situations I've Experienced

  1. If you do objects = BoardQuerySet.as_manager(), it works normally and 1 comes out. However, because select_related does not work, the number of queries increases from 10 to 20, so the speed decreases significantly.

  2. objects = BoardManager.from_queryset(BoardQuerySet)()
    When BoardManager.from_queryset(BoardQuerySet)() is used, deleted objects are not visible, but many queries occur because select_related is not applied.

Of course, the simplest solution would be to use Board.objects.filter(delted__isnull=False), but this is not an advantage of using the Manager. Please let me know how to solve it.

TMP Solution

class BoardManager(SafeDeleteManager):
    def get_queryset(self):
        qs = BoardQuerySet(self.model, self._db).exclude(deleted__isnull=False).select_related('user', 'board_category')
        qs = qs.order_by('-pk')
        return qs
  1. Available Board.objects.is_public().for_user(user=user)
  2. correct working return 1
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