问题
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