创建权限
from users.models import User
from django.contrib.auth.models import User,Permission,Group
from django.contrib.contenttypes.models import ContentType
# 决定在那个app.models.py
content_type = ContentType.objects.get_for_model(User)
# 添加权限
perm = Permission.objects.create(codename='muphone_perm',name='muphone_perm',content_type=content_type)
perm.save()
添加权限
# 找用户
userdata = User.objects.get(username="shenxiaojun")
# 找权限
perdata = Permission.objects.get(codename="muphone_perm")
# 添加到 user的关联表user_permissions 中
userdata.user_permissions.add(perdata)
查询权限
ret = Permission.objects.filter(content_type_id=7,id__gt=21)
修改
userdata = User.objects.get(username="shenxiaojun")
userdata.is_superuser = True #False
userdata.save()
标签:userdata,Permission,用户,perm,Django,objects,User,权限
From: https://www.cnblogs.com/snakej/p/17528422.html