3

我正在尝试创建一个自定义对象管理器,如https://docs.djangoproject.com/en/dev/topics/db/managers/#modifying-initial-manager-querysets所述

我正在做这样的事情:

# the model, say Alpha
class MyManager(Manager):
    pass
Alpha.objects = MyManager()

我认为不应该做任何事情。但是只要设置这个就可以了'NoneType' object has no attribute '_meta'。这怎么可能?我想我正在密切关注这个例子。

我检查并Alpha.objects在覆盖之前与之后的类型相同svGroup.objects.__class__.__bases__[0]()(因此它确实是子类的实例)。

我有一种感觉,这将是我更愚蠢的问题之一,但我无法弄清楚......

4

1 回答 1

3

It should be inside the model definition. Because it's handling with the __new__ method of model's metaclass.

class Alpha(models.Model):
    ...
    objects = MyManager()
于 2012-12-25T16:57:37.130 回答