python – Understanding the Django 1.7 Permissions System

Question:

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

Assuming you have an application whose app_label is foo and a model 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 added the permission: forms_and_models | author | Can add author to my user. Following the documentation model, 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 am I missing about the permissions system?

Answer:

I found out what was going on. 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 it using the command ./manage.py shell , which uses the shebang from the manage.py file, everything went according to the documentation. Permissions are now listed normally. I've never had problems like this using Python 3 to start the shell. Go understand.

Thanks!

Scroll to Top