python - 当用户有权限时,在管理员中获取 "Permission denied"页面

标签 python django permissions

我在 Django 中遇到了一些用户权限问题。我为 Magasin 模型添加了一些权限,如下所示:

add_magasin=Permission.objects.get(codename="add_magasin")
change_magasin=Permission.objects.get(codename="change_magasin")
delete_magasin=Permission.objects.get(codename="delete_magasin")
user.user_permissions.add(add_magasin)
user.user_permissions.add(change_magasin)
user.user_permissions.add(delete_magasin)
user.save()

然后当我检查我得到的权限时:

In [100]: user.has_perm(delete_magasin)
Out[100]: False

In [101]: user.has_perm(add_magasin)
Out[101]: False

In [102]: user.has_perm(change_magasin)
Out[102]: False

在管理员中,与同一用户连接,我可以添加一个 Magasin 实例,但不能删除一个实例(“权限被拒绝”)。我什至无法使用 super 用户删除 Magasin 实例。

我使用的是 Django 1.3,并且没有使用任何外部权限相关的应用程序...

编辑:sql查询

mysql> select * from django_content_type;
+----+-----------------------+--------------+------------------+
| id | name                  | app_label    | model            |
+----+-----------------------+--------------+------------------+
***more stuff***
|  9 | magasin               | securite_v2  | magasin          |
***more stuff***


mysql> select * from auth_permission;
+----+----------------------------------+-----------------+-------------------------+
| id | name                             | content_type_id | codename                |
+----+----------------------------------+-----------------+-------------------------+
***more stuff***
| 25 | Can add magasin                  |               9 | add_magasin             |
| 26 | Can change magasin               |               9 | change_magasin          |
| 27 | Can delete magasin               |               9 | delete_magasin          |
***more stuff***


mysql> select * from auth_user where id=135;
+-----+---------------+------------+-----------+-------------------------------+-----------------------------------------------------+----------+-----------+--------------+---------------------+---------------------+
| id  | username      | first_name | last_name | email                         | password                                            | is_staff | is_active | is_superuser | last_login          | date_joined         |
+-----+---------------+------------+-----------+-------------------------------+-----------------------------------------------------+----------+-----------+--------------+---------------------+---------------------+
| 135 | admingrandest |            |           | <a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="38595c5551565f4a59565c5d4b4c78404040165b5755" rel="noreferrer noopener nofollow">[email protected]</a> | sha1$14f21$02a50f37be16f27aba3f677618b663edfb0ce5a7 |        1 |         1 |            0 | 2012-06-25 11:16:35 | 2012-06-22 16:42:46 |
+-----+---------------+------------+-----------+-------------------------------+-----------------------------------------------------+----------+-----------+--------------+---------------------+---------------------+


mysql> select * from auth_user_user_permissions;
+----+---------+---------------+
| id | user_id | permission_id |
+----+---------+---------------+
|  1 |     135 |            25 |
|  2 |     135 |            26 |
|  3 |     135 |            27 |
***more stuff***

可能出了什么问题?

最佳答案

User.has_perm() 的第一个参数必须是 <app label>.<permission codename> 中的字符串格式。在您传递的代码中 Permission实例作为第一个参数,其计算结果为 <app_label> | <content_type> | <name> ,所以has_perm将始终返回 False .

而是使用 user.has_perm("<yourapp>.delete_magasin")

关于python - 当用户有权限时,在管理员中获取 "Permission denied"页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11186531/

相关文章:

android - 来自 MediaDocumentsProvider 的权限拒绝

python - GTK3+ 和 Python 中的线程同步

python - 是什么让 python webscrape 输出 unicode?

javascript - 如何从 views.py 获取值并在 html 模板中使用它?

Django 管理员 : show records for respective user only

django-tinymce 不显示格式化工具栏

windows - ubuntu 上的 cfdirectory 在 windows 上访问 smbfs 的奇怪问题

javascript - Firefox OS 设备或模拟器上的音频捕获

python - 如何合并/连接两个不同长度的 Pandas 数据帧?

python - 如何使用密码将 Superset 连接到 Redis?