Django BaseManager and DefaultManager clarification

Viewed 130

I am using Django 3.2. I am reading a section on Custom managers, and came across this text in the documentation:

By default, Django uses an instance of the Model._base_manager manager class when accessing related objects (i.e. choice.question), not the _default_manager on the related object. This is because Django needs to be able to retrieve the related object, even if it would otherwise be filtered out (and hence be inaccessible) by the default manager.

If the normal base manager class (django.db.models.Manager) isn’t appropriate for your circumstances, you can tell Django which class to use by setting Meta.base_manager_name.

Base managers aren’t used when querying on related models, or when accessing a one-to-many or many-to-many relationship. For example, if the Question model from the tutorial had a deleted field and a base manager that filters out instances with deleted=True, a queryset like Choice.objects.filter(question__name__startswith='What') would include choices related to deleted questions.

The section text seems a bit convoluted. Is it possible to explain what is being said in the above text, by using the models below?

class Foo(models.Model):
    name = models.CharField(max_length=64)
    deleted = models.Boolean(default=False)
    # ...

class FooBar(models.Model):
    foo = models.ForeignKey(Foo, on_delete=models.CASCADE, related_name='foos')
    start_date = models.DateTimeField(default=now, db_index=True)
    end_date = models.DatetTimeField(default=now+timeddelta(days=200), db_index=True)
    # ...
0 Answers
Related