Understanding the permissions system of Django 1.7

Posted on

Question :

I have an app called forms_and_models that I use for study. In the Django 1.7 documentation says:

  

Assuming you have an application whose app_label is foo and a template called Bar , to test basic permissions, you should 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 admin, I’ve added the permission: forms_and_models | author | Can add author to my user. Following the documentation template, I deduced that to test if 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 am I letting go unnoticed about the permissions system?

    

Answer :

I discovered what was happening. The problem was just that I was starting the interactive shell via terminal using Python 3, like this:
python3 manage.py shell

When I opened using the ./manage.py shell command, which uses the shebang of the manage.py file, everything happened as per the documentation. Permissions are now listed normally. I’ve never had problems like this using Python 3 to start the shell. Go understand.

Thank you!

    

Leave a Reply

Your email address will not be published. Required fields are marked *