Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display user group in admin interface?

Tags:

python

django

The admin.py at contrib.auth defines the fields to be displayed for the user at the admin interface:

list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff')

I want to see the group of each user here:

enter image description here

Just for testing purpose when you try to add the "group" field it fail:

list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff', 'group')

This error rises: : (admin.E109) The value of 'list_display[5]' must not be a ManyToManyField.

After searching I have found only ways to add an app to the admin interface, or to create a custom user model, but I think I'm missing something.

So, how to do it?

SOLVED --- Thanks to the answer of @{Alexis N-o}

Edit /usr/local/lib/pyton2.7/dist-packages/django/contrib/auth/admin.py

Just before the list_display add this def that seeks for the groups:

def group(self, user):
    groups = []
    for group in user.groups.all():
        groups.append(group.name)
    return ' '.join(groups)
group.short_description = 'Groups'

list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff', 'group')

Then syncdb, and check the change at admin, notice the last column:

enter image description here

like image 334
jaimeandrescatano Avatar asked Jul 02 '15 21:07

jaimeandrescatano


People also ask

What are groups in admin panel in Django?

Groups are a means of categorizing users. This allows for granting permissions to a specific group. It also saves a lot of time, as compared to granting permissions to each individual user. A user may belong to any number of groups and automatically has all the permissions granted to that group.

What are groups and users in Django?

Django does provide groups and permissions option but this is a model or table level and not at the object level. Hence we decided on creating groups based on the objects on which we want to provide the access and the users were added to these groups as per requirement or based on the existing state of an object.

How to create a group in django?

Django Admin Panel : In Admin Panel you will see Group in bold letter, Click on that and make 3-different group named level0, level1, level3 . Also, define the custom permissions according to the need. By Programmatically creating a group with permissions: Open python shell using python manage.py shell.


1 Answers

It is not possible by default because of the ManyToMany relation but this should work:

def group(self, user):
    groups = []
    for group in user.groups.all():
        groups.append(group.name)
    return ' '.join(groups)
group.short_description = 'Groups'

list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff', 'group')
# The last argument will display a column with the result of the "group" method defined above

You can test it and organize the code to your convenience.

like image 150
Alexis N-o Avatar answered Oct 08 '22 22:10

Alexis N-o