如何获得所有Django组并可选获取关联的用户记录?
这应该很简单,因为纯SQL非常简单。我有以下查询,它可以在Django中得到我想要的内容:
SELECT auth_group.id, auth_group.name, auth_user.username
FROM auth_group
LEFT JOIN auth_user_groups
ON auth_group.id = auth_user_groups.group_id
LEFT JOIN auth_user
ON auth_user_groups.user_id = auth_user.id
WHERE auth_user.id = 3
OR auth_user.id IS NULL;
如何正确使用ORM获得此结果?
This should be pretty simple since it's very simple with pure SQL. I have the following query which gets exactly what I want in Django:
SELECT auth_group.id, auth_group.name, auth_user.username
FROM auth_group
LEFT JOIN auth_user_groups
ON auth_group.id = auth_user_groups.group_id
LEFT JOIN auth_user
ON auth_user_groups.user_id = auth_user.id
WHERE auth_user.id = 3
OR auth_user.id IS NULL;
How can I get this result using the ORM properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以查询:
group
由此QuerySet产生的对象将具有额外的属性.username
带有用户名称。尽管这可能会引入很多重复的数据,但由于用户名是none
(null
),或使用id = 3 。
You can query with:
The
Group
objects that arise from this queryset will have an extra attribute.username
with the name of the user. Although this will introduce likely a lot of duplicate data, since the username will be eitherNone
(NULL
) or the username of the user withid=3
.