tags:

views:

47

answers:

1

Hi All,

I have gone through a strange behavior while creating a user, using Django admin interface. I have to create a user which can add other users, but for that Django requires two permissions i.e. add user and change user. But when I gave user the change permission, its even able to change the superuser of the site.

What I want is to create a user which can only create other users.

Please suggest.

Thanks in advance.

A: 

This isn't supported by default in Django. You could subclass the normal UserAdmin and make your own, that disables the "superuser"-checkbox for non-superusers:

from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
from django.contrib import admin

class MyUserAdmin(UserAdmin):

    def formfield_for_dbfield(self, db_field, **kwargs):
        field = super(MyUserAdmin, self).formfield_for_dbfield(db_field, **kwargs)
        user = kwargs['request'].user
        if not user.is_superuser:
            if db_field.name == 'is_superuser':
                field.widget.attrs = {'disabled': 'disabled'}
        return field

admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
lazerscience