1

I want to include two extra managers on the auth user model, active and inactive, to give me just active, or just inactive users. This is how the model would look (even if the it is invalid):

from django.contrib.auth.models import User

class ActiveManager(models.Manager):
    def get_query_set(self):
        return super(ActiveManager, self).get_query_set().filter(active=True)

class InactiveManager(models.Manager):
    def get_query_set(self):
        return super(InactiveManager, self).get_query_set().filter(active=False)

class User(models.Model):
    # user model...

    all_users = models.Manager()
    objects = ActiveManager()
    inactive = InactiveManager()

Where / how exactly would I place this so I can do a query such as User.inactive.all() ? Thank you.

Duncan Parkes
  • 1,902
  • 1
  • 15
  • 24
David542
  • 104,438
  • 178
  • 489
  • 842

1 Answers1

0

You're going to need to use the contribute_to_class method on your Manager. Instead of the User class you have there, you will need something like this:

InactiveManager.contribute_to_class(User, 'inactive')

I suspect it doesn't matter exactly where you do this as long as it happens nice and early (before you use it!) - a models.py somewhere would feel vaguely right.

Duncan Parkes
  • 1,902
  • 1
  • 15
  • 24