django - Custom many to one manager on self -


i'm trying use custom manager on model used in self relationship can add code add method. here's code:

from django.db import models  class testmodelmanager(models.manager):      use_for_related_fields = true      def __init__(self):         models.manager.__init__(self)      def add(self, *args, **kwargs):         print('my custom code.')         super().add(*args, **kwargs)  class testmodel(models.model):      objects = testmodelmanager()      name = models.charfield(max_length=50)     parent = models.foreignkey(         'self',         related_name='children',         null=true,         blank=true,     ) 

python shell output

>>> testmods.models import testmodel >>> testmodel.objects <testmods.models.testmodelmanager object @ 0x10bc96e80> >>> testmodel.objects.bulk_create([testmodel(name="foo"), testmodel(name="bar")]) [<testmodel: testmodel object>, <testmodel: testmodel object>] >>> t1, t2 = testmodel.objects.all() >>> t1.children.add(t2) >>> t1.children.all() <queryset [<testmodel: testmodel object>]> >>>  

in line t1.children.add(t2) why didn't print "my custom code."?

i found answer perusing source code. appears add method added custom manager , cannot modified in method described above.

def create_reverse_many_to_one_manager(superclass, rel):     """     create manager reverse side of many-to-one relation.     manager subclasses manager, default manager of     related model, , adds behaviors specific many-to-one relations.     """ 

Comments