What's the best way to store Phone number in Django models

后端 未结 8 2070
情歌与酒
情歌与酒 2020-11-27 09:12

I am storing a phone number in model like this:

phone_number = models.CharField(max_length=12)

User would enter a phone number

相关标签:
8条回答
  • 2020-11-27 09:28

    Validation is easy, text them a little code to type in. A CharField is a great way to store it. I wouldn't worry too much about canonicalizing phone numbers.

    0 讨论(0)
  • 2020-11-27 09:28

    Others mentioned django-phonenumber-field. To get the display format how you want you need to set PHONENUMBER_DEFAULT_FORMAT setting to "E164", "INTERNATIONAL", "NATIONAL", or "RFC3966", however you want it displayed. See the GitHub source.

    0 讨论(0)
  • 2020-11-27 09:31

    This solution worked for me:

    First install django-phone-field

    command: pip install django-phone-field
    

    then on models.py

    from phone_field import PhoneField
    ...
    
    class Client(models.Model):
        ...
        phone_number = PhoneField(blank=True, help_text='Contact phone number')
    

    and on settings.py

    INSTALLED_APPS = [...,
                      'phone_field'
    ]
    

    It looks like this in the end

    0 讨论(0)
  • 2020-11-27 09:37

    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.

    1. Phone by PhoneNumberField

    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

    In model:

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

    In form:

    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. 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.

    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."))
    

    EDIT

    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
    
    0 讨论(0)
  • 2020-11-27 09:38

    I will describe what I use:

    Validation: string contains more than 5 digits.

    Cleaning: removing all non digits symbols, write in db only numbers. I'm lucky, because in my country (Russia) everybody has phone numbers with 10 digits. So I store in db only 10 diits. If you are writing multi-country application, then you should make a comprehensive validation.

    Rendering: I write custom template tag to render it in template nicely. Or even render it like a picture - it is more safe to prevent sms spam.

    0 讨论(0)
  • 2020-11-27 09:39

    Use CharField for phone field in the model and the localflavor app for form validation:

    https://docs.djangoproject.com/en/1.7/topics/localflavor/

    0 讨论(0)
提交回复
热议问题