在Django中实现添加user到group并查看
作者:痴狂一生一世任逍遥 时间:2021-12-08 21:47:07
一、添加user到group
第一种:
user.groups.add(1) # add by id
第二种:
from django.contrib.auth.models import Group
my_group = Group.objects.get(name='my_group_name')
my_group.user_set.add(your_user)
二、查看group里面的用户
第一种:
group = Group.objects.get(name='blogger')
users = group.user_set.all()
第二种:
User.objects.filter(groups__name='blogger')
第三种:
perm = Permission.objects.get(codename='blogger')
users = User.objects.filter(Q(groups__permissions=perm) | Q(user_permissions=perm) ).distinct()
来源:https://blog.csdn.net/qq_38854847/article/details/89916234
标签:Django,user,group
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
基于Python实现一个简单的学生管理系统
2023-07-23 23:06:40
![](https://img.aspxhome.com/file/2023/7/82777_0s.jpg)
为你总结一些php系统类函数
2023-11-15 02:22:35
Python tkinter实现桌面软件流程详解
2022-03-26 09:51:32
![](https://img.aspxhome.com/file/2023/6/79596_0s.png)
Oracle 数据表分区的策略
2023-07-08 12:19:18
pycharm2022没有manage repositories配置镜像源的解决方法
2022-06-27 05:30:24
![](https://img.aspxhome.com/file/2023/9/78449_0s.png)
Python Decorator装饰器的创建方法及常用场景分析
2022-05-05 03:34:11
golang gorm中格式化时间问题详解
2023-07-12 09:45:21
python画图的函数用法以及技巧
2021-11-01 17:58:19
![](https://img.aspxhome.com/file/2023/1/67261_0s.png)
Yahoo! BrowserPlus 发布
2008-11-20 13:35:00
Python NumPy教程之二元计算详解
2023-12-16 18:25:04
豆瓣可以做而且值得做的几件事情
2009-04-24 12:07:00
收集的ORACLE函数大全
2010-07-16 12:58:00
python批量读取txt文件为DataFrame的方法
2021-09-29 12:04:21
![](https://img.aspxhome.com/file/2023/6/77786_0s.jpg)
Python3.9新特性详解
2023-03-26 21:56:16
![](https://img.aspxhome.com/file/2023/6/79426_0s.png)
python socket网络编程步骤详解(socket套接字使用)
2022-09-15 11:35:08
Google的YSlow——Page Speed(附插件下载)
2009-09-27 12:40:00
![](https://img.aspxhome.com/file/UploadPic/20099/27/page_speed_01-95s.gif)
Facebook:产品设计评价体系解密
2011-05-24 17:13:00
基于生活形态的用户分群研究
2009-12-30 16:54:00
![](https://img.aspxhome.com/file/UploadPic/20101/11/t1gzvrxlroxxxxxxxx-575-440-57s.jpg)
Go语言程序查看和诊断工具详解
2023-06-22 02:40:38
![](https://img.aspxhome.com/file/2023/8/89038_0s.jpg)
解决杀毒软件误删asp文件的方法
2008-10-21 21:23:00