0

I have an app called article. I want to hide articles with hidden = True. All hidden articles should not be findable except for admins.

I'm using a custom manager and I'm using a custom Modeladmin.

my custom modeladmin:

class ArticleAdmin(admin.ModelAdmin):
    def get_queryset(self, request):
        qs = super().get_queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(hidden=False)

my custom manager:

class ArticleManager(models.Manager):
    def get_queryset(self):
         qs = super().get_queryset()
         return qs.filter(hidden=False)

I want to get all articles with hidden = False when I use Article.objects.all() and when I'm in the admin panel I also want to get articles with hidden = True.

Admin panel -> All articles Article.objects.all() -> All articles with hidden = False

What's working: When I use Article.objects.all() I get all articles with hidden = False

What's not working: When I'm on the admin panel I don't see any articles with hidden = True.

Only in the admin panel articles with hidden = True should be visible. In Sitemaps, Querysets ... they shouldnt be returned.

Myzel394
  • 1,155
  • 3
  • 16
  • 40

1 Answers1

0

First of all, the better practice is to define QuerySet as a custom inner class and then pass it to a manager. Then you have various options - you can just define two querysets and use a specific one for your administrators or you can do User rights check directly in your manager class as it's done, for example, in answer to this question. Then you can just use objects.all() and it will return different results for different users.

Custom QuerySet and Manager without breaking DRY?

Hope it will help ^^

Igor Belkov
  • 446
  • 3
  • 8
  • Ok thanks. I defined `all` to give all articles with `hidden = False` and now I want to return all articles with `admin_all`. How do I call this in admin? – Myzel394 Feb 16 '19 at 09:40