简体   繁体   English

Django:覆盖用户查询集以从公众中过滤掉管理员/员工用户?

[英]Django: Override user queryset to filter out admin/staff users from the public?

I want to filter the user manager self.get_queryset() method in such a way that users on the client application don't see admin and staff users when searching for or viewing other accounts.我想过滤用户管理器 self.get_queryset() 方法,使客户端应用程序上的用户在搜索或查看其他帐户时看不到管理员和员工用户。 The issue I'm running into is I am unable to login with my auth system if I override get_queryset entirely.我遇到的问题是,如果我完全覆盖 get_queryset,我将无法使用我的身份验证系统登录。 My current setup is:我目前的设置是:

class AccountManager(BaseUserManager):
    def get_public_queryset(self):
        return self.get_queryset().filter(active=True, verified=True, admin=False, staff=False)



Using this design works fine if I define various sorting methods in the manager (because I can simply call that method), but it seems as though there should be a better way to do this.如果我在管理器中定义各种排序方法(因为我可以简单地调用该方法),使用这种设计效果很好,但似乎应该有更好的方法来做到这一点。 Any ideas?有任何想法吗?

I think additional methods, as you've implemented is good a solution, but if you insist using get_queryset method it would be fine to override the method and preserve base functionality.我认为您已经实施的其他方法是一个很好的解决方案,但是如果您坚持使用 get_queryset 方法,则可以覆盖该方法并保留基本功能。 I'd do something like this:我会做这样的事情:

...
def get_queryset(self, *a, **kw):
    queryset = super().get_queryset(*a, **kw)
    # filter your queryset here as you wish
    queryset = queryset.filter(active=True, verified=True, admin=False, staff=False)
    return queryset
...

As I've spotted from question text, you tried to call self.get_queryset() which would be recursive call (not super class implementation call), which will finally cause maximum recursion depth exceeded error.正如我从问题文本中发现的那样,您尝试调用 self.get_queryset() 这将是递归调用(不是超级 class 实现调用),这最终会导致最大递归深度超出错误。

Hope it helps希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM