django custom user model groups and permissions
Asked Answered
J

2

6

I am trying to implement custom user model in my django application.

If I just copy and paste the code from this article, it works great. But I wish this custom user model to have permissions and groups. So I added this inheritance to models.py:

class MyUser(AbstractBaseUser, PermissionsMixin):

and these fields into the admin.py MyUserAdmin(UserAdmin) class:

('Permissions', {'fields': (
    'is_admin', 'is_staff', 'is_active', 'groups', 'user_permissions',
)}),

But it looks strange for me:

admin screenshot

As I know, it must be two containers: left (that I have) that shows all available groups and permissions and right (that I don't have) that shows all current user's groups and permissions.

P.S. I tried google for it and found only one post on reddit which is 10 month old but doesn't have a solution.

Joachima answered 10/4, 2016 at 21:54 Comment(1)
did you get the answer?Provence
J
5

Deleting this line in MyUserAdmin class fixed the problem up.

filter_horizontal = ()
Joachima answered 12/4, 2016 at 17:5 Comment(0)
D
0

You must set argument in filter_horizontal(ARGUMENT), the argument is your field manyToMany in model.

Example:

class CustomUser(PermissionsMixin, AbstractBaseUser):
    custom_groups = models.ManyToManyField('CustomUserGroups', blank=True)  

class CustomUserAdmin(UserAdmin):
  filter_horizontal = ('custom_groups', )
Dronski answered 22/10, 2021 at 8:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.