Django扩展对象管理器引发'NoneType'对象没有属性'_meta' [英] Django extending objects manager raises 'NoneType' object has no attribute '_meta'
问题描述
我正在尝试按照 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'
.这怎么可能?我想我正在密切关注这个例子.
Which I think should not do anything. But just setting this raises 'NoneType' object has no attribute '_meta'
. How is this possible? I think I am following the example rather closely.
在覆盖之前,我检查并Alpha.objects
与之后的svGroup.objects.__class__.__bases__[0]()
类型相同(因此它确实是子类的实例).
I checked and Alpha.objects
before the overwrite is the same type as svGroup.objects.__class__.__bases__[0]()
afterwards (so it is indeed an instance of a subclass).
我觉得这将是我更愚蠢的问题之一,但我无法弄清楚...
I have a feeling this is going to be one of my more stupid questions but I can't figure it out...
推荐答案
它应该在模型定义中.因为它是通过模型的元类的__new__方法处理的.
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()
这篇关于Django扩展对象管理器引发'NoneType'对象没有属性'_meta'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!