Override Django User Manager to only return active users in requests

Requires a method for calling User.objects.filter/get to return user objects with is_active equal to True.

I tried to define a user manager and monkey patch it on a user model, for example:

 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() 

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 want to say that I know that fixing a monkey like this is very bad in terms of maintainability - it will mean that it will be revised later, but now we need a quick fix.

Here's the full stack trace if someone wants this 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' 
+4
source share
2 answers

Do I need to apply a filter to a user model? Why don't you try to subclass this model instead ?, and the manager code for this.

 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') 

or use a proxy model

+1
source

Found the answer in this post . Since his question is specific, and mine is general, I do not consider them duplicate, but the answer to his question also answers my questions.

0
source

All Articles