AUTHENTICATION_BACKENDS = (
...
# Needed to login by username in Django admin, regardless of `allauth`
'django.contrib.auth.backends.ModelBackend',
# `allauth` specific authentication methods, such as login by e-mail
'allauth.account.auth_backends.AuthenticationBackend',
...
)
from django.contrib.auth.models import AbstractUser
from django.core.validators import RegexValidator
from django.db import models
from django.utils.translation import gettext_lazy as _
from django_countries.fields import CountryField
class CustomUser(AbstractUser):
display_name = models.CharField(verbose_name=_("Display name"), max_length=30, help_text=_("Will be shown e.g. when commenting"))
date_of_birth = models.DateField(verbose_name=_("Date of birth"), blank=True, null=True)
address1 = models.CharField(verbose_name=_("Address line 1"), max_length=1024, blank=True, null=True)
address2 = models.CharField(verbose_name=_("Address line 2"), max_length=1024, blank=True, null=True)
zip_code = models.CharField(verbose_name=_("Postal Code"), max_length=12, blank=True, null=True)
city = models.CharField(verbose_name=_("City"), max_length=1024, blank=True, null=True)
country = CountryField(blank=True, null=True)
phone_regex = RegexValidator(regex=r"^\+(?:[0-9]●?){6,14}[0-9]$", message=_("Enter a valid international mobile phone number starting with +(country code)"))
mobile_phone = models.CharField(validators=[phone_regex], verbose_name=_("Mobile phone"), max_length=17, blank=True, null=True)
additional_information = models.CharField(verbose_name=_("Additional information"), max_length=4096, blank=True, null=True)
photo = models.ImageField(verbose_name=_("Photo"), upload_to='photos/', default='photos/default-user-avatar.png')
class Meta:
ordering = ['last_name']
def __str__(self):
return f"{self.username}: {self.first_name} {self.last_name}"
Django 다국어 지원
from django.utils.translation import gettext_lazy as _
verbose_name=_('안녕하세요.')
Django 추가 필드 설치
pip install django-countries
3. forms.py
from .models import CustomUser
from wagtail.users.forms import UserCreationForm, UserEditForm
class WagtailUserCreationForm(UserCreationForm):
class Meta(UserCreationForm.Meta):
model = CustomUser
widgets = {'date_of_birth': forms.DateInput(attrs={'type':'date'})}
class WagtailUserEditForm(UserEditForm):
class Meta(UserEditForm.Meta):
model = CustomUser
widgets = {'date_of_birth': forms.DateInput(attrs={'type':'date'})}
{% extends "wagtailusers/users/create.html" %}
{% block extra_fields %}
{% include "wagtailadmin/shared/field_as_li.html" with field=form.display_name %}
{% include "wagtailadmin/shared/field_as_li.html" with field=form.date_of_birth %}
{% include "wagtailadmin/shared/field_as_li.html" with field=form.address1 %}
{% include "wagtailadmin/shared/field_as_li.html" with field=form.address2 %}
{% include "wagtailadmin/shared/field_as_li.html" with field=form.zip_code %}
{% include "wagtailadmin/shared/field_as_li.html" with field=form.city %}
{% include "wagtailadmin/shared/field_as_li.html" with field=form.country %}
{% include "wagtailadmin/shared/field_as_li.html" with field=form.mobile_phone %}
{% include "wagtailadmin/shared/field_as_li.html" with field=form.additional_information %}
{% include "wagtailadmin/shared/field_as_li.html" with field=form.photo %}
{% endblock extra_fields %}