10 examples of 'django phone number field' in Python

Every line of 'django phone number field' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
236def test_defer_phone_number_field(self):
237 m = models.MandatoryPhoneNumber.objects.create(phone_number=self.test_number_1)
238 m = models.MandatoryPhoneNumber.objects.defer("phone_number").get(pk=m.pk)
239 self.assertEqual(m.phone_number, self.test_number_1)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

184def clean_phone(self):
185 """Ensure phone number is valid and that person can be invited."""
186 phone = super(InviteFamilyForm, self).clean_phone()
187 try:
188 self.instance = model.Profile.objects.get(phone=phone)
189 except model.Profile.DoesNotExist:
190 self.instance = None
191 else:
192 # is this parent connected to any other (non-orphan) students?
193 students = model.Profile.objects.filter(
194 relationships_to__from_profile=self.instance,
195 ).filter(relationships_to__from_profile__school_staff=True)
196 if self.rel:
197 students = students.exclude(pk=self.rel.to_profile_id)
198 if students.exists():
199 raise forms.ValidationError(
200 u"This person is already connected to a different student. "
201 u"Portfoliyo doesn't support family members in multiple "
202 u"villages yet, but we're working on it!"
203 )
204 return phone
16def to_internal_value(self, data):
17 phone_number = to_python(data)
18 if phone_number and not phone_number.is_valid():
19 raise ValidationError(self.error_messages['invalid'])
20 return phone_number
73def to_local_formatted_phone(self, value):
74 return self._get_formatted_phone(
75 value, phonenumbers.PhoneNumberFormat.NATIONAL
76 )
37def __init__(self, *args, min_length=10, **kwargs):
38 kwargs.setdefault('max_length', 14)
39
40 if min_length < 10:
41 self.min_length = 10
42 else:
43 self.min_length = min_length
44
45 if kwargs['max_length'] < self.min_length:
46 kwargs['max_length'] = self.min_length
47
48 super(ModelPhoneField, self).__init__(*args, **kwargs)
49 self.validators.append(validators.MinLengthValidator(self.min_length))
144def get_phone(self):
145 return phonenumbers.parse(self.phone, self.country)
62def test_international_display_format(self):
63 form_class = self.init_form(
64 region='FI',
65 display_format='international'
66 )
67 form = form_class(MultiDict(phone_number='0401234567'))
68 assert 'value="+358 40 1234567"' in form.phone_number()
42def clean(self, value):
43 super(PTPhoneNumberField, self).clean(value)
44 if value in EMPTY_VALUES:
45 return ''
46 value = re.sub('(\.|\s)', '', smart_text(value))
47 m = phone_digits_re.search(value)
48 if m:
49 return '%s' % value
50 raise ValidationError(self.error_messages['invalid'])
43def a_phone_number(dummy_number='xxx-xxx-xxxx'):
44 return ''.join([random.choice(string.digits) if char == 'x' else char for char in dummy_number])
413def test_error_message(self):
414 class PhoneNumberForm(forms.Form):
415 number = formfields.PhoneNumberField()
416
417 form = PhoneNumberForm({"number": "invalid"})
418 self.assertIs(form.is_valid(), False)
419 self.assertEqual(
420 form.errors, {"number": ["Enter a valid phone number (e.g. +12125552368)."]}
421 )

Related snippets