自定义权限中不存在权限匹配查询

时间:2017-09-11 05:45:51

标签: python django django-models permissions django-views

try:
        user = User.objects.get(username = 'xyz')
        custom_permission = Permission.objects.get(codename='is_custom')
        user.user_permissions.add(custom_permission)
        user.save()
        print user.has_perm("is_custom")
        print user.has_perm('app.is_custom')
        user.get_all_permissions()

    except Exception as e:
        print(">>>>>",e)

我已经做了这么简单的事情来检查用户自定义权限,但它会引发异常

DoesNotExist('权限匹配查询不存在。')

这有什么不对吗?

2 个答案:

答案 0 :(得分:0)

如果没有Model对象满足给定条件,

<div onclick="play_fn()"> click here to play </div> <br> <script> function play_fn(){ $(#vid).play(); } </script> <iframe id="vid" width="560" height="315" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true" allowFullScreen="true" src="https://www.facebook.com/video/embed?video_id=10157271716931509"> </iframe>应返回上述错误。即,没有代号为get的Permission对象。您必须修改您的查询,例如

is_custom

答案 1 :(得分:0)

我已经创建了此权限

from django.db import models class RightsSupport(models.Model): class Meta: managed = False permissions = (('DELETE', 'can delete'),)

在saparerte文件中并迁移。这在数据库中创建了这样的东西

Appname | YourClassContaingPermission |许可名称

所以在我的情况下

app |权利| DELETE

所以现在如果我这样检查

<强> request.user.has_perm( 'app.DELETE')

返回 true

相关问题