'How add group for custom user in django?
I created custom model of user using this official docs customizing authentication in Django But how can add groups and premissions? My django is version 1.9
Solution 1:[1]
You can use groups and permissions with your custom user model by using the PermissionsMixin (https://docs.djangoproject.com/en/3.1/topics/auth/customizing/#custom-users-and-permissions)
Just inherit the PermissionsMixin with your custom user model like so:
class CustomUser(AbstractBaseUser, PermissionsMixin):
Then you can access it in exactly the same way you would with the default django.contrib.auth User model.
Solution 2:[2]
TL; DR
('Group Permissions', {
'fields': ('groups', 'user_permissions', )
})
Detailed Explanation
Add this in admin.py
app/admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .models import App
class AppAdminConfig(UserAdmin):
# ordering, list_display, etc..
fieldsets = (
# Other fieldsets
('Group Permissions', {
'classes': ('collapse',),
'fields': ('groups', 'user_permissions', )
})
)
admin.site.register(App, AppAdminConfig)
If anyone is interested how entire code looks like, see this.
Solution 3:[3]
As a complement to @ajinzrathod answer: I applied the solution and got error because I missed some trailing commas. The working code in admin.py is as following:
class UserAdminConfig(UserAdmin):
model = CustomUser
fieldsets = (
# Other fieldsets
('Group Permissions', {
'fields': ('groups', 'user_permissions', )
}),
)
admin.site.register(CustomUser, UserAdminConfig)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | MPatel1 |
| Solution 2 | |
| Solution 3 | Sean William |
