Customize username and password field in Django?
Question: First, How can I set `min_length` for `username`?
`ChachaUser._meta.get_field('username').min_length = 2` doesn't work.

Second, How can I place `placeholder` for `password1` and `password2`?
`forms.PasswordInput(attrs={'placeholder' : "6자리 이상"}),` doesn't work.

This is my customized `User` model and `UserCreationForm`.

`models.py`

    
    
    from django.core.validators import RegexValidator
    from django.contrib.auth.models import AbstractUser
    from django.db import models
    
    
    GENDER_CHOICES = (
        ('M', '남'),
        ('F', '여'),
    )
    
    phone_regex = RegexValidator(
        regex=r'^\d{11}$',
        message=" '-' 없이 입력해주세요",
    )
    
    username_regex = RegexValidator(
        regex=r'^[0-9a-zA-Z]*$',
        message='아이디는 영어와 숫자로만 구성되어야 합니다.'
    )
    
    
    
    class ChachaUser(AbstractUser):
    
        birth = models.DateField("생년월일")
        name = models.CharField(
            "이 름",
            max_length=4
        )
        gender = models.CharField(
            "성 별",
            max_length=1,
            choices=GENDER_CHOICES,
            default='M'
        )
    
        phone_number = models.CharField(
            "핸드폰",
            validators=[phone_regex],
            max_length=11
        )
    
        job = models.CharField(
            "직 업",
            max_length=20,
        )
    
        # python manage.py createsuperuser 할 때 나오는 항목
        REQUIRED_FIELDS = [
            'birth',
            'name',
            'gender',
            'phone_number',
            'job',
            'email'
        ]
    
    ChachaUser._meta.get_field('username').verbose_name = '아이디'
    ChachaUser._meta.get_field('username').validators = [username_regex]
    ChachaUser._meta.get_field('username').max_length = 20
    ChachaUser._meta.get_field('username').min_length = 2
    

`forms.py`

    
    
    from django import forms
    from django.contrib.auth.forms import UserCreationForm
    from django.contrib.auth import get_user_model
    
    
    class MyUserCreationForm(UserCreationForm):
    
        birth = forms.DateField(
            label="생년월일",
            widget=forms.SelectDateWidget(
                years=range(1970, 2015)
            ),
        )
    
        class Meta(UserCreationForm.Meta):
            model = get_user_model()
            fields = UserCreationForm.Meta.fields + (
                'name',
                'gender',
                'birth',
                'phone_number',
                'job',
            )
            exclude = ('email', )
            widgets = {
                'username' : forms.TextInput(
                    attrs={'placeholder': '알파벳, 숫자만 가능(20자 이내)'}
                ),
                'phone_number' : forms.TextInput(
                    attrs={'placeholder' : "ex) 01012341234"}
                ),
                'job' : forms.TextInput(
                    attrs={'placeholder' : "ex) 한국대 철학과, 선생님 등(20자 이내)"}
                ),
                'password1' : forms.PasswordInput(
                    attrs={'placeholder' : "6자리 이상"}
                ),
                'password2' : forms.PasswordInput(
                    attrs={'placeholder' : "6자리 이상"}
                ),
            }
    


Answer: For first question:

    
    
    from django.core import validators    
    class ChachaUser(AbstractUser):
        ..................
        AbstractUser._meta.get_field('username').validators=[validators.MinLengthValidator(2)]
    

For second question your couldn't change `password1` in `Meta.widgets` because
it is not a model field. You can override widget in `__init__` method instead.

