我想在我的模型中存储用户的电话号码,但不知道以哪种格式存储它。
存储电话号码的最佳做法是什么?如何通过注册表单(以及通过序列化程序等)轻松处理它?我是 Django 新手,所以我需要一些关于此的建议。
我想在我的模型中存储用户的电话号码,但不知道以哪种格式存储它。
存储电话号码的最佳做法是什么?如何通过注册表单(以及通过序列化程序等)轻松处理它?我是 Django 新手,所以我需要一些关于此的建议。
您应该使用正则表达式验证器。您不需要 3rd 方库。
# models.py
from django.core.validators import RegexValidator
# this is your user model
class YourUser(AbstractUser):
# error message when a wrong format entered
phone_message = 'Phone number must be entered in the format: 05999999999'
# your desired format
phone_regex = RegexValidator(
regex=r'^(05)\d{9}$',
message=phone_message
)
# finally, your phone number field
phone = models.CharField(validators=[phone_regex], max_length=60,
null=True, blank=True)