This works for me in my table / project model:
@permission_required('myApp.add_project') def create(request):
Obviously change add_project to add_ [regardless of your model / table]. To edit it:
@permission_required ('myApp.edit_project')
and delete:
@permission_required ('myApp.delete_project')
But I found that the main thing is to make sure your authentication tables are configured correctly. This caused me problems. Here is a MySQL SQL query that I wrote to check permissions if you use groups. This should work in most dB:
select usr.id as 'user id',usr.username,grp.id as 'group id',grp.name as 'group name',grpu.id as 'auth_user_groups',grpp.id as 'auth_group_permissions',perm.name,perm.codename from auth_user usr left join auth_user_groups grpu on usr.id = grpu.user_id left join auth_group grp on grpu.group_id = grp.id left join auth_group_permissions grpp on grp.id = grpp.group_id left join auth_permission perm on grpp.permission_id = perm.id order by usr.id;
I found that my rights were not configured correctly, and also keep an eye on the django_content_type table, which should contain rows for each application and a table for each add, edit, delete. Therefore, if you have a project table, you should see this in django_content_type:
id [generated by dB] app_label myApp model project
If you are having problems, another good idea is to enable and use the django admin application. This will show you where your problems are, and by setting some testing permissions, users and groups, you can then examine the tables discussed above to see what is inserted there. This will give you an idea of how authorization permissions work.
I write this to save someone from having to spend several hours figuring out what I did!
Grahamj
source share