1
votes

The problem:

I would like to use django admin site User model to register new users. The default option offers you to register new username with password and password confirmation, which are required fields.

My idea is to make few changes. I would like to keep username field and add email-field, however delete password and password confirmation. The principal idea is, I would register a user and he would receive an email with generated password. He would then be able to login with generated password and change the password.

I am facing huge problems with templates, errors and most of all overriding the classes.

I have spent many many hours checking django documentation, goolge and the best I could do so far is:

admin.py

from django.contrib import admin
from django.contrib.auth import admin as upstream
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth.models import Group, User
from django.utils.translation import ugettext, ugettext_lazy as _


class UserAdmin(upstream.UserAdmin):
    fieldsets = (
        (None, {'fields': ('username', 'password','email')}),
        (_('Personal info'), {'fields': ('first_name', 'last_name')}),
        (_('Permissions'), {'fields': ('is_active', 'is_staff', 'is_superuser',
                                       'groups', 'user_permissions')}),
        (_('Important dates'), {'fields': ('last_login', 'date_joined')}),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('username','email')}
        ),
    )

    form = UserChangeForm
    add_form = UserCreationForm

try:
    admin.site.unregister(User)
except NotRegistered:
    pass

admin.site.register(User, UserAdmin)

This is a code I have found on stackoverflow. Within add_fieldsets I removed password1, and password2. If I go to admin page, everything looks great, however I can not register a user.

I think it is because of UserCreationForm, which insists of creating an password, but perhaps I could be wrong. Can anyone please let me know how to override UsercreationForm, so passwords wouldn't be mandatory anymore and errors from UserCreationForm would not be seen on the site.

2
well yeah, as a beginner you did get someting wrong. You shouldn't be using the admin app for this. docs.djangoproject.com/en/1.11/topics/authe4c5
I created my own model with cascade delete on User. You entered a username, email and saved those data to USER. Howerver, the problem was, if I made a change to my_own model, this didn't affect USER model.Testing man
Again I don't see why you need to add a delete cascade tot he user model. Usually it's the reference for most other models (ie the opposite of what you have coded)e4c5
I just want to use Admin app if it is already there... it offers a lot of flexibility in further development so I would really like to use Admin app.Testing man

2 Answers

2
votes

Customizing Django auth can be very difficult and that's why I would recommend looking into 3rd party apps to help you achieve your goals. One you might want to check out is: https://github.com/bee-keeper/django-invitations

0
votes

even though it is an old question and no real answer, one way to do most of the stuff (without knowing how to act on the "send password") would be:

from django.contrib.auth.admin import UserAdmin as DjangoUserAdmin
from django.contrib.auth.forms import UserCreationForm as DjangoUserCreationForm


class UserCreationForm(DjangoUserCreationForm):
    password1 = None
    password2 = None

    def clean(self):
        password = get_random_string() # here you want to maybe send a signal which can be picked up or something
        self.cleaned_data['password1'] = password
        self.cleaned_data['password2'] = password

        return super().clean()


class UserAdmin(DjangoUserAdmin):
    add_form = UserCreationForm

    fieldsets = (
        (None, {'fields': ('username',)}),
        (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}),
    )
    add_fieldsets = (
        (None, {
            'classes': ('wide',),
            'fields': ('username', 'email'),
        }),
    )
    list_filter = tuple()
    list_display = ('username', 'email', 'first_name', 'last_name')


admin.site.register(User, UserAdmin)