12

django internal code uses _base_manager instead of objects

There is also _default_manager

I'm more accustomed to using objects

What's the difference?

eugene
  • 39,839
  • 68
  • 255
  • 489
  • I would very much like to upvote this question but my policy prohibits me when the author has not accepted an answer that gives a solution. – raratiru Nov 05 '17 at 16:45

1 Answers1

13

The difference between 'objects' and '_base_manager' is that you can replace 'objects' with custom manager, but '_base_manager' will be the default django.db.models.Manager instance anyway. In general, you should'n be using '_base_manager'. Sometimes Django itself needs to use '_base_manager' to be sure of it's behavior.

from django.db import models


class CommentManager(models.Manager):
    pass


class Comment(models.Model):
    ...
    objects = CommentManager()

print(type(Comment.objects)) #<class 'main.models.CommentManager'>
print(type(Comment._default_manager)) #<class 'main.models.CommentManager'>
print(type(Comment._base_manager)) #<class 'django.db.models.manager.Manager'>

To explain '_default_manager' I'll give one more simple example:

class Comment(models.Model):
    ...
    custom_objects = CommentManager()

print(type(Comment._default_manager)) #<class 'main.models.CommentManager'>
print(type(Comment._base_manager)) #<class 'django.db.models.manager.Manager'>
print(type(Comment.objects)) #AttributeError: type object 'Comment' has no attribute 'objects'

So, is you set custom manager to model, it won't have 'objects' attribute, but it still will have '_default_manager'(your custom manager instance) and '_base_manager' - django.db.models.Manager instance.

Also be aware that there's a bug in Django source code related to managers according to my ticket: https://code.djangoproject.com/ticket/25897 I provided the patch to fix it, but it hasn't been applied yet.

Alex Polekha
  • 1,590
  • 15
  • 13
  • 1
    The bug mentioned in the last para was fixed 2 years ago in Django 1.8. Otherwise: very helpful, thanks Alex. +1 for showing the code to highlight differences instead of just describing it. – thclark Apr 26 '18 at 17:10