https://www.pythoneatstail.com/en/overview-all-articles/add-extra-fields-custom-user-model/
1. admin.py
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.forms import UserCreationForm, UserChangeForm
from .models import CustomUser
class CustomUserAdmin(UserAdmin):
add_form = UserCreationForm
form = UserChangeForm
model = CustomUser
list_display = ['pk', 'email', 'username', 'first_name', 'last_name']
add_fieldsets = UserAdmin.add_fieldsets + (
(None, {'fields': ('email', 'first_name', 'last_name', 'display_name', 'date_of_birth', 'address1', 'address2', 'zip_code', 'city', 'country', 'mobile_phone', 'additional_information', 'photo',)}),
)
fieldsets = UserAdmin.fieldsets + (
(None, {'fields': ('display_name', 'date_of_birth', 'address1', 'address2', 'zip_code', 'city', 'country', 'mobile_phone', 'additional_information', 'photo',)}),
)
admin.site.register(CustomUser, CustomUserAdmin)
- fieldsets
- admin페이지에서 유저 수정할때 나타나는 입력폼 무엇으로 할건지
- add_fieldsets
- admin페이지에서 유저 추가할때 나타나는 입력폼 무엇으로 할건지\
- list_display
- admin 리스트에서 보여지는 필드
2. models.py
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'})}
4. base.py
os.path.join(BASE_DIR, 'userauth/templates/userauth/'),
AUTH_USER_MODEL = 'userauth.CustomUser'
WAGTAIL_USER_CREATION_FORM = 'userauth.forms.WagtailUserCreationForm'
WAGTAIL_USER_EDIT_FORM = 'userauth.forms.WagtailUserEditForm'
WAGTAIL_USER_CUSTOM_FIELDS = ['display_name', 'date_of_birth', 'address1', 'address2', 'zip_code', 'city', 'country', 'mobile_phone', 'additional_information', 'photo',]
5. html
{% 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 %}
'Back-End > PythonEatsTail' 카테고리의 다른 글
06 Signup and password reset with allauth in Django (0) | 2021.12.09 |
---|---|
05 Git and local settings in Django (0) | 2021.12.08 |
04 Setting up validated login with allauth in Django (0) | 2021.12.08 |
03 Setting up allauth in Django (0) | 2021.12.08 |
01 Wagtail with a custom user model (0) | 2021.12.07 |