#django #django-allauth
#django #django-allauth
Вопрос:
Пытаюсь добавить нового пользователя в группу «general_user». поскольку я использую custom user model
и django-allauth
создал пользовательскую форму регистрации, наследуя allauth SignupForm
. он отлично работает на веб-странице, но не на странице администратора. каждый раз, когда вы пытаетесь нажать добавить Customuser, он показывает ошибку ниже.
что я должен проверить для этого?
forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from django.contrib.auth import authenticate, get_user_model
from allauth.account.forms import SignupForm
from django.contrib.auth.models import Group
class RegistrationForm(SignupForm):
email = forms.EmailField(
max_length=60,
help_text='Required, Enter avalid Email address.'
)
class Meta:
model = get_user_model()
fields = ['email', 'username', 'password1', 'password2']
def custom_signup(self, request, user):
custom_form = self #use local signup()
if hasattr(custom_form, 'signup') and callable(custom_form.signup):
custom_form.signup(request, user)
def signup(self, request, user):
general_user_group = Group.objects.get(name='general_user')
user.groups.add(general_user_group)
user.save()
return user
admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth import get_user_model
from .forms import CustomUserChangeForm, RegistrationForm
CustomUser = get_user_model()
class CustomUserAdmin(UserAdmin):
form = CustomUserChangeForm
add_form = RegistrationForm
model = CustomUser
list_display = ('email', 'username', 'is_staff',)
list_filter = ('is_staff', 'is_active',)
search_fields = ('email', 'username',)
ordering = ('email',)
readonly_fields = ('date_joined', 'last_login')
fieldsets = (
(None, {'fields': ('email', 'password',)}),
('Personal info', {'fields': ('username', )}),
('Permissions', {
'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions',),
}),
('Important dates', {'fields': ('last_login', 'date_joined',)}),
)
# for Creating user form
add_fieldsets = (
(None, {
'classes': ('wide',),
'fields': ('email', 'username', 'password1', 'password2',),
}),
)
filter_horizontal = ('groups', 'user_permissions',)
admin.site.register(CustomUser, CustomUserAdmin)
settings.py
ACCOUNT_FORMS = {'signup': 'accounts.forms.RegistrationForm'}
docker,
django 3.15,
python 3.8