Django extending objects manager raises 'NoneType' object has no attribute '_meta'

[亡魂溺海] 提交于 2019-12-10 20:55:45

问题


I am trying to create a custom objects manager as described https://docs.djangoproject.com/en/dev/topics/db/managers/#modifying-initial-manager-querysets

I am doing something like this:

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

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.

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...


回答1:


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()


来源:https://stackoverflow.com/questions/14032329/django-extending-objects-manager-raises-nonetype-object-has-no-attribute-met

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!