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
Asked
Active
Viewed 1.0k times
3 Answers
20
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.
-
4Thank you. This answer was helpful. Just I want to add one more in here for newbie. If you inherit PermissionsMixin after you already have done migration and migrate with your custom user model, It doesn't create relationship between your custom user model and group or permissions. I was confused for awhile because after inheriting PermissionMixin, I couldn't find tables in db for relationship between user and Group or user and Permission. – Jayground Nov 15 '16 at 07:01
-
1Did you register Group model through `admin.site.register(Group)` ? – Yasser Mohsen Jun 05 '20 at 10:55
11
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.

ajinzrathod
- 925
- 10
- 28
-
Could you details a bit more your answer, in which class this data structure should live? – jlandercy Jul 09 '21 at 08:22
-
Check this. Hope this will clear. If you still unclear, will help you out. Link: https://pastebin.com/mk4XsEHj Skip to line 36 – ajinzrathod Jul 09 '21 at 08:28
-
1Thank you for being responsive. Do you mind to update your answer and just add class nesting and fields name (not the complete code you linked). Goal is to understand in which class and which member this data structure should live. You links is great but you know it breaks. I'll vote up then, I am sure it will help other user! Regards – jlandercy Jul 09 '21 at 08:53
-
0
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)

William Le
- 825
- 1
- 9
- 16