FieldError at /admin/accounts/customuser/add/
Unknown field(s) (usable_password) specified for CustomUser. Check fields/fieldsets/exclude attributes of class CustomUserAdmin.
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/accounts/customuser/add/
Django Version: 5.1.1
Exception Type: FieldError
Exception Value:
Unknown field(s) (usable_password) specified for CustomUser. Check fields/fieldsets/exclude attributes of class CustomUserAdmin.
Exception Location: C:\Users\Baxti\AppData\Local\Programs\Python\Python312\Lib\site-packages\django\contrib\admin\options.py, line 841, in get_form
Raised during: django.contrib.auth.admin.add_view
Python Executable: C:\Users\Baxti\AppData\Local\Programs\Python\Python312\python.exe
Python Version: 3.12.5
Python Path:
['C:\\Users\\Baxti\\Desktop\\Django-CRM',
'C:\\Users\\Baxti\\AppData\\Local\\Programs\\Python\\Python312\\python312.zip',
'C:\\Users\\Baxti\\AppData\\Local\\Programs\\Python\\Python312\\DLLs',
'C:\\Users\\Baxti\\AppData\\Local\\Programs\\Python\\Python312\\Lib',
'C:\\Users\\Baxti\\AppData\\Local\\Programs\\Python\\Python312',
'C:\\Users\\Baxti\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages']
Server time: Thu, 26 Sep 2024 06:46:21 +0000
accounts/admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from .forms import CustomUserCreationForm, CustomUserChangeForm
from .models import CustomUser
class CustomUserAdmin(UserAdmin):
add_form = CustomUserCreationForm
form = CustomUserChangeForm
model = CustomUser
list_display = ['email','username','first_name','last_name','age','is_staff']
fieldsets = UserAdmin.fieldsets + (
(None, {'fields': ('age',)}),
)
add_fieldsets = UserAdmin.add_fieldsets + (
(None,{'fields':('age',)}),
)
admin.site.register(CustomUser, CustomUserAdmin)
accounts/models.py
from django.contrib.auth.models import AbstractUser
from django.db import models
class CustomUser(AbstractUser):
age = models.PositiveIntegerField(null=True, blank=True)
accounts/forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser
from django.contrib.auth.models import User
class CustomUserCreationForm(UserCreationForm):
class Meta(UserCreationForm):
model = CustomUser
fields = ('username', 'first_name', 'last_name', 'email',)
class CustomUserChangeForm(UserChangeForm):
class Meta:
model = CustomUser
fields = ('first_name','last_name','email','age',)
class SignUpForm(UserCreationForm):
email = forms.EmailField(label="", widget=forms.TextInput(attrs={'class':'form-control', 'placeholder':'Email'}))
first_name = forms.CharField(label="", max_length=100, widget=forms.TextInput(attrs={'class':'form-control', 'placeholder':'Ism'}))
last_name = forms.CharField(label="", max_length=100, widget=forms.TextInput(attrs={'class':'form-control', 'placeholder':'Familiya'}))
class Meta:
model = User
fields = ('username', 'first_name', 'last_name', 'email', 'password1', 'password2')
def __init__(self, *args, **kwargs):
super(SignUpForm, self).__init__(*args, **kwargs)
self.fields['username'].widget.attrs['class'] = 'form-control'
self.fields['username'].widget.attrs['placeholder'] = 'Username'
self.fields['username'].label = ''
self.fields['username'].help_text = '<span class="form-text text-muted"><small>Qabul qiladi 150 belgilar yoki kamroq. Harflar, raqamlar, va faqat @/./+/-/_ belgialar. </small></span>'
self.fields['password1'].widget.attrs['class'] = 'form-control'
self.fields['password1'].widget.attrs['placeholder'] = 'Parol'
self.fields['password1'].label = ''
self.fields['password1'].help_text = ('<ul class="form-text text-muted small">'
'<li>Sizning parol ma\'lumotlaringiz bilan bir hil bo\'lmasin!</li>'
'<li>Sizning parol 8 belgidan kam bo\'lmasin!</li>'
'<li>Sizning parol juda ham oddiy bo\'lmasligi lozim!</li>'
'<li>Sizning parol faqat raqamlardan tashkil topmasligi lozim!</li></ul>')
self.fields['password2'].widget.attrs['class'] = 'form-control'
self.fields['password2'].widget.attrs['placeholder'] = 'Parol takroran'
self.fields['password2'].label = ''
self.fields['password2'].help_text = '<span class="form-text text-muted"><small>Tastiqlash uchun. Parolni takroran yozing. </small></span>'
accounts/view.py
from django.shortcuts import render, redirect
from django.contrib.auth import login, authenticate
from django.urls import reverse_lazy
from django.contrib import messages
from django.views.generic import CreateView
from .forms import CustomUserCreationForm, SignUpForm
class SignUpView(CreateView):
form_class = CustomUserCreationForm
success_url = reverse_lazy('login')
template_name = 'registration/register.html'
def register_user(request):
if request.method == 'POST':
form = SignUpForm(request.POST)
if form.is_valid():
form.save()
# Authenticate and login
username = form.cleaned_data['username']
password = form.cleaned_data['password1']
user = authenticate(username=username, password=password)
login(request, user)
messages.success(request, "Siz muvaffaqiyatli ro`yhatdan o`tdingiz!")
return redirect('home')
else:
form = SignUpForm()
return render(request, 'register.html', {'form':form})
return render(request, 'register.html', {'form':form})