3
I have an app called forms_and_models which I use for study. In documentation of Django 1.7 says that:
Assuming you have an application whose app_label is foo and a model called Bar, to test basic permissions, you must use:
- to add:
user.has_perm('foo.add_bar')
- to change:
user.has_perm('foo.change_bar')
- to delete:
user.has_perm('foo.delete_bar')
Through the administrator, I added the permission: forms_and_models | Author | Can add Author to my user. Following the documentation template, I deduced that to test whether a user has permission to add an author, I could run the following code:
"""O usuário abaixo existe e é o que eu configurei a permissão acima"""
user = User.objects.get(username='junior')
user.has_perm('forms_and_models.add_author')
However this line of code always returns false. What I’m missing goes unnoticed over the permissions system?
I tested your code here and it seems to work correctly (I used Django 1.4 however, but the result should be the same). Try
user.get_all_permissions()
and see what happens. By the way, is this user a superuser or not, team member or not, active or not? (for example, if it is inactive,get_all_permissions
will list permissions correctly, buthas_perm
will returnFalse
)– mgibsonbr
@mgibsonbr is active yes, it is not a superuser nor a member of the team. The result of
user.get_all_permissions()
returnsset()
. Any more information I can give?– juniorgarcia
I have no idea what might be going on, so... :( On my test,
user.get_all_permissions()
gaveset([u'forms_and_models.add_author'])
. When you return there in admin, appears this permission to your user?– mgibsonbr
Yes, I can save whatever permissions are for that user, only the command line will not :(
– juniorgarcia
Dumb question: You closed the command line and opened it again (or at least picked up the BD user again in a new query
get
) after saving the user via admin?– mgibsonbr
@mgibsonbr found what was wrong and answered my question, take a look down there. After you tested my code and everything went well, it came to mind that I should test with the Python 2 shell by Manage.py (i was starting with Python 3). Thanks for the help :)
– juniorgarcia