【发布时间】:2017-11-30 16:16:56
【问题描述】:
我想要完成的是在管理员中合并未知数量的查询集。我有一个用户可以查看的作者列表,根据用户在列表中的作者,他应该只能看到他们的文章。我所拥有的是:
def get_queryset(self, request):
#getting all the lists and doing not important stuff
return (qs.filter(author__name = list(list_of_authors)[0]) | qs.filter(author__name = list(list_of_authors)[len(list_of_authors)-1])).distinct()
如果用户可以查看两位作者的文章,则此方法有效,但是对于三位作者,它不起作用。我尝试使用:
for index in list_of_authors:
return qs.filter(author__name = list(list_of_authors)[index])
Author 类有一个name = Charfield(max_length=50)。
遗憾的是,我只得到了最后一个查询集。当数量未知时甚至可以合并查询集,因为经过大量搜索后我最终没有找到任何东西。
【问题讨论】:
-
能否展示相关模型,尤其是作者字段定义?
-
我编辑了这个问题,因为有一个小错误。
标签: python django django-queryset