我在 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 实例,但不能删除一个(“权限被拒绝”)。我什至无法使用超级用户删除 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 | | | admingrandest@xxx.com | 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***
有什么问题?