5

I have declared a manager to the following model, but after doing so, I can no longer use List.objects.get(). Anybody know why?

class List(models.Model):
  title = models.CharField(max_length=20, unique=True)
  archived = models.BooleanField()

  archived_lists = ArchivedListManager()
  active_lists = ActiveListManager()

And the managers:

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

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

The error is type object 'List' has no attribute 'objects'

Josh
  • 4,427
  • 5
  • 28
  • 27

2 Answers2

20

As noted in the Django docs:

If you use custom Manager objects, take note that the first Manager Django encounters (in the order in which they're defined in the model) has a special status. Django interprets the first Manager defined in a class as the "default" Manager, and several parts of Django will use that Manager exclusively for that model. As a result, it's a good idea to be careful in your choice of default manager in order to avoid a situation where overriding get_query_set() results in an inability to retrieve objects you'd like to work with.

So as far as "why" goes, it's to allow you to provide your own default manager.

The solution is simple, though: just add this

objects = models.Manager()

to your model class.

mipadi
  • 398,885
  • 90
  • 523
  • 479
0

Adding your own manager suppresses creation of the stock manager.

"Manager names"

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • I made this manager "class ListManager(models.Manager): def get_query_set(self): return super(ListManager, self).get_query_set()" which prints the correct objects with List.objects, but it fails with "List.objects.get(example=example) How can I fix that? – Josh Dec 15 '10 at 22:00
  • That sounds like a completely separate question. – Ignacio Vazquez-Abrams Dec 15 '10 at 22:01