对querys不起作用

2024-05-16 02:44:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试使用Django中内置的Groups系统将用户是否属于组传递给模板:

def is_contributor(request):

    group = Group.objects.get_or_create(name='contributor')

    return {
        'is_contributor': True if group in request.user.groups.all() else False
    }

即使用户是组的一部分,以下内容也将通过False。具体来说,如果我通过以下步骤:

request.user.groups.all()

我得到:

<QuerySet [<Group: contributor>]>

这让我相信这个案子的“in”不是在查询集上起作用

在查询集中使用in是否有限制?有更好的方法吗


Tags: django用户infalseisrequest系统group