Queries regarding Custom Managers with model inheritance.

8 views
Skip to first unread message

Dheerendra Rathor

unread,
Jun 9, 2016, 4:39:49 PM6/9/16
to Django users
I was going through https://docs.djangoproject.com/en/1.9/topics/db/managers/#custom-managers-and-model-inheritance regarding custom managers and I decided to do some tests. Here are my models:

from django.db import models


class CustomManager(models.Manager):

    use_for_related_fields = True


class ExtraManager(models.Manager):
    use_for_related_fields = True


class AnotherManager(models.Manager):
    use_for_related_fields = True


class A(models.Model):

    objects = CustomManager()

    all_objects = AnotherManager()

    class Meta:
        abstract = True


class B(A):
    pass


class C(A):
    objects = ExtraManager()


class D(C):
    pass


class E(A):
    all_objects = AnotherManager()
    objects = ExtraManager()


class F(E):
    pass


Here are the results of few code executions:

In [1]: from app.models import *

In [2]: F.objects
Out[2]: <django.db.models.manager.Manager at 0x7f160dfe8e48>

In [3]: D.objects
Out[3]: <app.models.ExtraManager at 0x7f160dfe80b8>

In [4]: D._default_manager
Out[4]: <app.models.AnotherManager at 0x7f160dfe8978>

In [5]: F._de
F._default_manager  F._deferred         

In [5]: F._default_manager
Out[5]: <django.db.models.manager.Manager at 0x7f160dfe8e48>

In [6]: F.all_objects
Out[6]: <app.models.AnotherManager at 0x7f160dfe87f0>



Here I have few queries:
- How come AnotherManager is default Manage for D? It is neither default manager for C and neither it is first Manager in A.
 
- Why D.objects is ExtraManager (from C objects) but F.objects is default Django Manager

Someone please help to understand theses. Thanks!
Reply all
Reply to author
Forward
0 new messages