PhoneNumberField

sihwan_e·2021년 6월 8일
0

Django

목록 보기
21/21

모델에 폰번호를 단순히 charfield나 integerfield로 는 넣기 싫어서 제대로 해보기로 했다.

설치

pip install django-phonenumber-field
pip install phonenumbers

추가

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'phonenumber_field',

모델

class Member(models.Model):
    username = models.CharField(max_length=10, verbose_name='이름')
    user_id = models.CharField(max_length=10, unique=True, verbose_name='아이디')
    password = forms.CharField(widget=forms.PasswordInput)
    email = models.EmailField(max_length=128)
    phone_num = PhoneNumberField(unique=True)
    register_date = models.DateTimeFieldregister_date = models.DateField(
        auto_now_add=True, verbose_name='등록일')

    def __str__(self):
        return self.user_id

    class Meta:
        db_table = 'members'
        verbose_name = '회원'
        verbose_name_plural = '회원'

예제

>>> a = Member(username='lee', user_id='lee12',email='lee12@n.com',phone_num='+821012341234')
>>> a.save()
>>> print(a.phone_num
... )
+821012341234
>>> a.phone_num
PhoneNumber(country_code=82, national_number=1012341234, extension=None, italian_leading_zero=None, number_of_leading_zeros=None, country_code_source=1, preferred_domestic_carrier_code=None)
>>> 
profile
Sometimes you gotta run before you can walk.

0개의 댓글