为什么向 Django 模型声明管理器会导致“对象”无效?
我已经为以下模型声明了一个管理器,但是这样做之后,我无法再使用List.objects.get()
。有人知道为什么吗?
class List(models.Model):
title = models.CharField(max_length=20, unique=True)
archived = models.BooleanField()
archived_lists = ArchivedListManager()
active_lists = ActiveListManager()
经理们:
class ArchivedListManager(models.Manager):
def get_query_set(self):
return super(ArchivedListManager, self).get_query_set().filter(archived=True)
class ActiveListManager(models.Manager):
def get_query_set(self):
return super(ActiveListManager, self).get_query_set().filter(archived=False)
错误是类型对象“List”没有属性“objects”
I have declared a manager to the following model, but after doing so, I can no longer use List.objects.get()
. Anybody know why?
class List(models.Model):
title = models.CharField(max_length=20, unique=True)
archived = models.BooleanField()
archived_lists = ArchivedListManager()
active_lists = ActiveListManager()
And the managers:
class ArchivedListManager(models.Manager):
def get_query_set(self):
return super(ArchivedListManager, self).get_query_set().filter(archived=True)
class ActiveListManager(models.Manager):
def get_query_set(self):
return super(ActiveListManager, self).get_query_set().filter(archived=False)
The error is type object 'List' has no attribute 'objects'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如 Django 文档中所述:
就“为什么”而言,它是为了允许您提供自己的默认管理器。
不过,解决方案很简单:只需将其添加
到您的模型类中即可。
As noted in the Django docs:
So as far as "why" goes, it's to allow you to provide your own default manager.
The solution is simple, though: just add this
to your model class.
添加您自己的管理器会抑制创建库存管理器。
“经理名称”
Adding your own manager suppresses creation of the stock manager.
"Manager names"