在 Django 模型中存储电话号码的最佳方法是什么 [英] What's the best way to store Phone number in Django models

查看:346
本文介绍了在 Django 模型中存储电话号码的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 model 中存储一个电话号码,如下所示:

I am storing a phone number in model like this:

phone_number = models.CharField(max_length=12)

用户将输入一个电话号码,我将使用该电话号码进行 SMS 身份验证 此应用程序将在全球范围内使用.所以我还需要国家代码.CharField 是存储电话号码的好方法吗?还有,我如何验证电话号码?

User would enter a phone number and I would use the phone number for SMS Authentication This application would be used globally. So I would also need country code. Is CharField a good way to store phone number? And, how do I validate the phone number?

推荐答案

您可能实际上会研究国际标准化格式 E.164, 例如由 Twilio 推荐(他们有一个服务和一个 API 用于通过 REST 请求发送 SMS 或电话).

You might actually look into the internationally standardized format E.164, recommended by Twilio for example (who have a service and an API for sending SMS or phone-calls via REST requests).

这可能是最通用的电话号码存储方式,尤其是在您使用国际号码的情况下.

This is likely to be the most universal way to store phone numbers, in particular if you have international numbers work with.

您可以使用 phonenumber_field 库.它是 Google 的 libphonenumber 库的端口,它支持 Android 的电话号码处理https://github.com/stefanfoulis/django-phonenumber-field

You can use phonenumber_field library. It is port of Google's libphonenumber library, which powers Android's phone number handling https://github.com/stefanfoulis/django-phonenumber-field

在模型中:

from phonenumber_field.modelfields import PhoneNumberField

class Client(models.Model, Importable):
    phone = PhoneNumberField(null=False, blank=False, unique=True)

形式:

from phonenumber_field.formfields import PhoneNumberField
class ClientForm(forms.Form):
    phone = PhoneNumberField()

从对象字段中获取电话作为字符串:

Get phone as string from object field:

    client.phone.as_e164 

标准化电话字符串(用于测试和其他工作人员):

Normolize phone string (for tests and other staff):

    from phonenumber_field.phonenumber import PhoneNumber
    phone = PhoneNumber.from_string(phone_number=raw_phone, region='RU').as_e164

2.通过正则表达式拨打电话

您的模型的一个注意事项:E.164 数字的最大字符长度为 15.

2. Phone by regexp

One note for your model: E.164 numbers have a max character length of 15.

要进行验证,您可以使用某种格式组合,然后尝试立即联系号码进行验证.

To validate, you can employ some combination of formatting and then attempting to contact the number immediately to verify.

我相信我在我的 django 项目中使用了类似以下内容:

I believe I used something like the following on my django project:

class ReceiverForm(forms.ModelForm):
    phone_number = forms.RegexField(regex=r'^+?1?d{9,15}$', 
                                error_message = ("Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed."))

<小时>

编辑

看来这篇文章对某些人很有用,将下面的评论整合到一个更完整的答案中似乎是值得的.根据 jpotter6,您也可以在模型上执行以下操作:

It appears that this post has been useful to some folks, and it seems worth it to integrate the comment below into a more full-fledged answer. As per jpotter6, you can do something like the following on your models as well:

models.py:

from django.core.validators import RegexValidator

class PhoneModel(models.Model):
    ...
    phone_regex = RegexValidator(regex=r'^+?1?d{9,15}$', message="Phone number must be entered in the format: '+999999999'. Up to 15 digits allowed.")
    phone_number = models.CharField(validators=[phone_regex], max_length=17, blank=True) # validators should be a list

这篇关于在 Django 模型中存储电话号码的最佳方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆