int
int

Reputation: 224

Django Admin. New user. Custom permissions

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.

Upvotes: 0

Views: 976

Answers (1)

Odif Yltsaeb
Odif Yltsaeb

Reputation: 5656

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)

Upvotes: 1

Related Questions