Skip to content

Permissions and Groups

In this video, we are going to learn the concept of permissions and groups in django.

Resources

Permission

  • In Django, permissions are a key component of the authentication and authorization system.

  • Permissions define the actions that users can perform on specific resources or objects.

  • Permissions allow you to control access and restrict certain operations to authorized users only.

  • By default django creates 4 types of permissions for every model. These default permissions include add, change, delete, view.

  • If a user has a add permission for a certain model, then that user can create new instances of the model.

  • If a user has change permission for a certain model, then that user can modify existing instances of the model.

  • Similarly, if a user has delete permission for a certain model, then that user can remove instances of the model.

  • Likewise, if a user has delete permission for a certain model, then that user can view instances of the model.

  • You can also create custom permissions to meet your business requirements. Custom permissions allow you to define more granular control over the actions users can perform. For example, you can define a custom permission like "publish_article" that grants users the ability to publish articles in a blogging application.

Code Setup

  • Now, lets setup the project code for this section.

  • First of all, you need to download the project code files the resources section: DOWNLOAD ZIP FILE

  • Now, extract the folder on the desktop.

  • Then, copy the mysite folder which is the project root directory.

  • Replace the mysite folder of your project with the copied mysite folder. So, if you made any mistake while writing the code in the previous section, then, it won't affect your learning in this section.

  • Open your project in the VS code and in the terminal, activate the virtual environment, and move to the mysite.

  • Run the migrations and create a superuser.

Demo

  • Open two browser and in one create a new user account by signing up from the signup page.
  • In another browser, Login as superuser and give permission to add, change, view and delete vaccine. Also give staff status for that user.
  • Explain the permissions granted to new user account

Groups

Now, lets us discuss the concept of groups in django.

  • Imagine you have 100 users in the database and you want to grant only add and view permission to those 100 users, not delete or change permissions.

  • In this case, you have two options. The first option is you will open every user profile and grant them permissions. But this approach is very time consuming to grant and revoke permissions if needed.

  • The second option, is creating a group. Basically, what you will do, You will create a group and then assign some permissions to that group. Now, every user who belongs to that group will be granted that group permissions by default. In this way, if you need to update the permission for a group of users, then it becomes very easy to manage group permissions instead of manually managing each and every user permissions.

Demo

  • Create a Group for Vaccine Administrator, Center Administrator, Campaign Administrator, Vaccination Administrator and assign respective permissions.
  • Assign the new user to some group and explain it.