覆盖Django用户管理器,只返回查询中的活动用户 [英] Override Django User Manager to only return active users in queries

查看:234
本文介绍了覆盖Django用户管理器,只返回查询中的活动用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要调用 User.objects.filter / get 才能返回具有 is_active 的用户对象

Need a way for calls to User.objects.filter/get to only return User objects with is_active set to True.

我尝试将自定义管理器和猴子修改到用户模型上,如下所示:

I tried to define a custom Manager and monkey patch it onto the User model, like this:

class CustomUserManager(UserManager):

    def get_query_set(self):
        return super(CustomUserManager, self).get_query_set().
          filter(is_active=True)

User.objects = CustomUserManager()
User.objects_all = UserManager()

但是,当我尝试调用User.objects.get()时,我得到:

AttributeError :'NoneType'对象没有属性'_meta'

But, when I try to make a call to User.objects.get(), I get:
AttributeError: 'NoneType' object has no attribute '_meta'

在我进一步之前,我想说我知道猴子这样的补丁在可维护性方面是非常糟糕的形式 - 这将被标记为稍后重新审查,但现在我们需要一个快速的解决方案。

Before I go any further, I'd want to say that I know that monkey patching like this is very bad form in terms of maintainability -- this will be marked to be revisited later, but right now we need a quick solution.

这是完整的堆栈跟踪,如果有人也希望这样做:

Here's the full stack trace, if anyone wants that as well:

  File "<console>", line 1, in <module>
  File "/Users/zarathustra/Virtual_Envs/fierce-spring-7383/venv/lib/python2.7/site-packages/django/db/models/manager.py", line 131, in get
    return self.get_query_set().get(*args, **kwargs)
  File "/Users/zarathustra/Virtual_Envs/fierce-spring-7383/venv/lib/python2.7/site-packages/django/db/models/query.py", line 358, in get
    clone = self.filter(*args, **kwargs)
  File "/Users/zarathustra/Virtual_Envs/fierce-spring-7383/venv/lib/python2.7/site-packages/django/db/models/query.py", line 621, in filter
    return self._filter_or_exclude(False, *args, **kwargs)
  File "/Users/zarathustra/Virtual_Envs/fierce-spring-7383/venv/lib/python2.7/site-packages/django/db/models/query.py", line 639, in _filter_or_exclude
    clone.query.add_q(Q(*args, **kwargs))
  File "/Users/zarathustra/Virtual_Envs/fierce-spring-7383/venv/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1250, in add_q
    can_reuse=used_aliases, force_having=force_having)
  File "/Users/zarathustra/Virtual_Envs/fierce-spring-7383/venv/lib/python2.7/site-packages/django/db/models/sql/query.py", line 1114, in add_filter
    opts = self.get_meta()
  File "/Users/zarathustra/Virtual_Envs/fierce-spring-7383/venv/lib/python2.7/site-packages/django/db/models/sql/query.py", line 233, in get_meta
    return self.model._meta
AttributeError: 'NoneType' object has no attribute '_meta'


推荐答案

需要将过滤器应用于用户模型?为什么不尝试子代这个模型?,并为这个代码编写一个经理。

It is a requirement to apply the filter to the User model? Why don't you try to subclass this model instead?, and code a manager for this one.

class CustomUserManager(UserManager):

    def get_query_set(self):
        return super(CustomUserManager, self).get_query_set().
          filter(is_active=True)


class MyUser(User):
    objects = CustomUserManager()

# get an active user which username is 'foo'
MyUser.objects.get(username='foo')

或使用代理模型

这篇关于覆盖Django用户管理器,只返回查询中的活动用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆