[英]Django Manager for Group model does not work - returns empty queryset
I'm junior dev. 我是初级开发人员。 I want to create managers for Django Groups.
我想为Django组创建管理员。 One new one and one that will override default manager EDIT: Django 1.8, python 2.7.15
一个新的将覆盖默认管理器的一个:Django 1.8,python 2.7.15
My managers: 我的经理:
class DefaultGroupManager(models.Manager):
def get_queryset(self):
test_ids = Test.objects.values_list('rel_group_id', flat=True)
return super(DefaultGroupManager, self).get_queryset().exclude(id__in=test_ids)
class AllGroupsManager(models.Manager):
def get_queryset(self):
return super(AllGroupsManager, self).get_queryset().exclude(rel_group__start_date__lte=datetime.now()-timedelta(days=30))
With these managers I created something like this: 与这些经理一起,我创建了以下内容:
dgm = DefaultGroupManager()
agm = AllGroupsManager()
agm.contribute_to_class(Group, 'get_all')
dgm.contribute_to_class(Group, 'objects')
And it was working. 它正在工作。 I could use
Group.get_all.all()
and new Group.objects.all()
. 我可以使用
Group.get_all.all()
和新的Group.objects.all()
。 In return I had proper lists of objects. 作为回报,我有适当的对象列表。
But my senior dev said that I have to do it by creating the new Group model that inherits from Group. 但是我的资深开发人员说,我必须通过创建继承自Group的新Group模型来做到这一点。 So I did:
所以我做了:
My Group model: 我的群组模型:
class GroupModel(Group):
get_all = DefaultGroupManager()
objects = AllGroupsManager()
But it does not work! 但这行不通!
When I use GroupModel.get_all.all()
or overrided GroupModel.objects.all()
it returns empty list []
instead of list with loads of objects. 当我使用
GroupModel.get_all.all()
或重写的GroupModel.objects.all()
它返回空列表[]
而不是带有对象加载的列表。
Everything seems to be good :( 一切似乎都很好:(
I will appreciate any help! 我将不胜感激!
If you're defining a new class, you definitely want to make it a proxy for Group. 如果要定义一个新类,则肯定要使其成为Group的代理 。 Otherwise it will have its own database table, which as you've found won't have any data in it.
否则它将有自己的数据库表,如您所见,该表中没有任何数据。
class GroupModel(Group):
get_all = DefaultGroupManager()
objects = AllGroupsManager()
class Meta:
proxy = True
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.