0
0
Djangoframework~10 mins

Built-in permission system in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Built-in permission system
Define Model
Django auto-creates permissions
Assign permissions to Users/Groups
Check permissions in views/templates
Allow or deny access based on permission
Django creates default permissions for each model, which you assign to users or groups. Then you check these permissions to control access.
Execution Sample
Django
from django.contrib.auth.models import User
user = User.objects.get(username='alice')
if user.has_perm('app.view_model'):
    print('Access granted')
else:
    print('Access denied')
This code checks if user 'alice' has the permission to view a model in 'app'.
Execution Table
StepActionEvaluationResult
1Get user 'alice'User object fetcheduser = User object
2Check permission 'app.view_model'user.has_perm('app.view_model')True or False
3If TruePrint 'Access granted'Output: Access granted
4If FalsePrint 'Access denied'Output: Access denied
💡 Permission check ends with either access granted or denied message
Variable Tracker
VariableStartAfter Step 1After Step 2Final
userNoneUser object for 'alice'User object for 'alice'User object for 'alice'
permission_checkNoneNoneTrue or FalseTrue or False
outputNoneNoneNone'Access granted' or 'Access denied'
Key Moments - 3 Insights
Why does Django create permissions automatically for models?
Django auto-creates 'add', 'change', 'delete', and 'view' permissions for each model to simplify access control, as shown in the concept flow after defining the model.
How does user.has_perm('app.view_model') know if permission is granted?
It checks the user's assigned permissions and group permissions in the database, as seen in step 2 of the execution table.
What happens if the user does not have the permission?
The code prints 'Access denied' as shown in step 4 of the execution table, preventing unauthorized access.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'permission_check' after Step 2?
AAlways True
BTrue or False depending on user permissions
CUser object
DNone
💡 Hint
Check the 'permission_check' variable in variable_tracker after Step 2
At which step does the program decide what message to print?
AStep 1
BStep 2
CStep 3 or Step 4
DAfter Step 4
💡 Hint
Look at the 'Action' and 'Result' columns in execution_table rows 3 and 4
If the user is not found, what would happen in this code?
AAn error occurs at Step 1
BPermission check runs normally
COutput is 'Access granted'
DOutput is 'Access denied'
💡 Hint
Step 1 fetches the user object; if user does not exist, it raises an error before permission check
Concept Snapshot
Django creates default permissions (add, change, delete, view) for each model.
Assign these permissions to users or groups.
Use user.has_perm('app.permission_codename') to check permissions.
Control access in views or templates based on these checks.
If permission is missing, deny access gracefully.
Full Transcript
Django's built-in permission system automatically creates four permissions for each model: add, change, delete, and view. These permissions can be assigned to users or groups to control what actions they can perform. In code, you retrieve a user object and check if they have a specific permission using user.has_perm('app.permission_codename'). Depending on the result, you allow or deny access, for example by printing 'Access granted' or 'Access denied'. This system helps keep your app secure by controlling access based on assigned permissions.