与Django文档中一样
默认情况下,Django为每个Django模型类添加一个名为 objects 的Manager
Django如何为模型类添加属性?
它是继承自models.Model
还是Django
动态添加objects=models.Manager()
到每个模型?
答案 0 :(得分:0)
除非您自己添加一个,否则它会动态添加到每个模型类中。
def _prepare(cls):
"""Create some methods once self._meta has been populated."""
opts = cls._meta
opts._prepare(cls)
...
if not opts.managers:
if any(f.name == 'objects' for f in opts.fields):
raise ValueError(
"Model %s must specify a custom Manager, because it has a "
"field named 'objects'." % cls.__name__
)
manager = Manager()
manager.auto_created = True
cls.add_to_class('objects', manager)