Django Admin. New user. Custom permissions

972 views Asked by At

I need to create custom template for 'new user' page in django admin (which is /admin/auth/user/add/ by default).

I only want to add some checkboxes, which represent custom permissions for user.

For example, if admin selects some of them during creation of new user, then new user will have those permissions. If none of them are selected, no permissions are assigned to new user.

How can i achieve this functionality?

Note: I do not need to change default user model.

1

There are 1 answers

0
Odif Yltsaeb On BEST ANSWER

Just change user creation form for admin and work your magic there:

from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserChangeForm

class CustomUserChangeForm(UserChangeForm):
    def save(self, force_insert=False, force_update=False, commit=True):
        m = super(CustomUserChangeForm, self).save(commit=False)
        # do custom stuff
        if commit:
            m.save()
        return m

class CustomUserAdmin(UserAdmin):
    form = CustomUserChangeForm

admin.site.unregister(User)
admin.site.register(User, CustomUserAdmin)