I’m trying to make custon user, and it’s going well, but when i try to create new user in admin page i dont have password field to input my password, instead i have “no password set” like below
here my full code
class CustomManager(BaseUserManager):
def _create_user(self, email, username, password, **extra_fields):
values = [email, username]
field_value_map = dict(zip(self.model.REQUIRED_FIELDS, values))
for field_name, value in field_value_map.items():
if not value:
raise ValueError('The {} value must be set'.format(field_name))
email = self.normalize_email(email)
user = self.model(
email=email,
username=username,
**extra_fields
)
user.set_password(password)
user.save(using=self._db)
Profile.objects.create(user=user)
return user
def create_user(self, email, username, password=None, **extra_fields):
extra_fields.setdefault('is_staff', False)
extra_fields.setdefault('is_superuser', False)
return self._create_user(email, username, password, **extra_fields)
def create_superuser(self, email, username, password=None, **extra_fields):
extra_fields.setdefault('is_staff', True)
extra_fields.setdefault('is_superuser', True)
if extra_fields.get('is_staff') is not True:
raise ValueError('Superuser must have is_staff=True.')
if extra_fields.get('is_superuser') is not True:
raise ValueError('Superuser must have is_superuser=True.')
return self._create_user(email, username, password, **extra_fields)
class CustomUser(AbstractBaseUser,PermissionsMixin):
username=models.CharField(max_length=150,unique=True)
email = models.EmailField(
max_length=255,
unique=True,
)
is_active = models.BooleanField(default=True)
is_admin = models.BooleanField(default=False)
is_staff = models.BooleanField(default=False)
last_login = models.DateTimeField(null=True)
date_joined = models.DateTimeField(default=timezone.now)
objects = CustomManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email']
class UserCreationForm(forms.ModelForm):
password = forms.CharField(label='Password', widget=forms.PasswordInput)
password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
class Meta:
model = CustomUser
fields = ('email', 'username','password', 'is_staff', 'is_superuser',)
def clean_password2(self):
# Check that the two password entries match
password1 = self.cleaned_data.get("password")
password2 = self.cleaned_data.get("password2")
if password1 and password2 and password1 != password2:
raise forms.ValidationError("Passwords don't match")
return password2
def save(self, commit=True):
# Save the provided password in hashed format
user = super().save(commit=False)
user.set_password(self.cleaned_data["password"])
if commit:
user.save()
return user
class UserChangeForm(forms.ModelForm):
password = ReadOnlyPasswordHashField()
class Meta:
model = CustomUser
fields = ('email', 'username', 'password', 'is_active', 'is_superuser')
def clean_password(self):
# Regardless of what the user provides, return the initial value.
# This is done here, rather than on the field, because the
# field does not have access to the initial value
return self.initial["password"]
@admin.register(CustomUser)
class CustomUserAdmin(admin.ModelAdmin):
form = UserChangeForm
add_form = UserCreationForm
list_display = ['username', 'email', 'password', ]
list_filter = ('is_superuser',)
fieldsets = (
(None, {'fields': ('email', 'is_staff', 'is_superuser', 'password')}),
('Personal info', {'fields': ('username', 'last_login','date_joined')}),
('Groups', {'fields': ('groups',)}),
('Permissions', {'fields': ('user_permissions',)}),
)
add_fieldsets = (
(None, {'fields': ('email', 'is_staff',
'is_superuser', 'password1', 'password2')}),
('Personal info', {'fields': ('username', )}),
('Groups', {'fields': ('groups',)}),
('Permissions', {'fields': ('user_permissions',)}),
)
search_fields = ('email', 'username',)
ordering = ('email',)
filter_horizontal = ()
I noticed that my add_fieldsets do nothing and instead all fields to create a user come from ‘fieldsets’ and I don’t know the reason because even the official documentation using this approach