删除用户权限(Django)

2024-09-29 18:57:49 发布

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

我有一个问题,删除用户在视图中,甚至在shell中的权限。我来解释一下我的问题:

我在壳里做了测试:

org = Organisateur.objects.get(user__username__contains="ghj") 
content_type = ContentType.objects.get_for_model(Tournoi)

图诺是一个模特的名字

^{pr2}$

但当我写下:

org.user.has_perm('inscription.ecriture_Palaiseau')` 

它返回True

但当我重写:

org = Organisateur.objects.get(user__username__contains="ghj")
org.user.has_perm('inscription.ecriture_Palaiseau')`

它返回False

真的很奇怪。为什么会这样工作?在

在我看来,即使我写了以下内容,权限也不会被删除:

org = Organisateur.objects.get(user__username__contains="ghj")

(删除权限后,用户仍然拥有权限)

我要做的是从一个用户中删除一个权限,然后立即向同一个用户添加另一个权限。 但每次我这么做,用户仍然有“删除权限”。。。。。。在

非常感谢

我期待着很快收到你们的来信。在


Tags: 用户org权限getobjectsusernamehascontains
1条回答
网友
1楼 · 发布于 2024-09-29 18:57:49

此行为是预期的,因为权限已缓存。来自Django文档:

Permission caching

The ModelBackend caches permissions on the User object after the first time they need to be fetched for a permissions check. This is typically fine for the request-response cycle since permissions are not typically checked immediately after they are added (in the admin, for example). If you are adding permissions and checking them immediately afterward, in a test or view for example, the easiest solution is to re-fetch the User from the database.

相关问题 更多 >

    热门问题