From 230f468ea0ad1dc3bdf74f6fc0fb35d26bde1ce9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Freitag?= Date: Wed, 1 Dec 2021 18:34:36 +0100 Subject: [PATCH] Make formfields.PhoneNumberField honor PHONENUMBER_DEFAULT_REGION Reviewed-by: stefanfoulis --- CHANGELOG.rst | 3 +++ phonenumber_field/formfields.py | 3 ++- tests/tests.py | 8 ++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 351cfafc..aa5a109a 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,6 +4,9 @@ CHANGELOG UNRELEASED ---------- +* Set the default region of ``phonenumber_field.formfields.PhoneNumberField`` + to ``PHONENUMER_DEFAULT_REGION``. + 6.0.0 (2021-10-20) ------------------ diff --git a/phonenumber_field/formfields.py b/phonenumber_field/formfields.py index e151c03c..0e487af0 100644 --- a/phonenumber_field/formfields.py +++ b/phonenumber_field/formfields.py @@ -1,4 +1,5 @@ import phonenumbers +from django.conf import settings from django.core import validators from django.core.exceptions import ValidationError from django.forms.fields import CharField @@ -17,7 +18,7 @@ def __init__(self, *args, region=None, **kwargs): self.widget.input_type = "tel" validate_region(region) - self.region = region + self.region = region or getattr(settings, "PHONENUMBER_DEFAULT_REGION", None) if "invalid" not in self.error_messages: if region: diff --git a/tests/tests.py b/tests/tests.py index be21502d..c08d66fe 100644 --- a/tests/tests.py +++ b/tests/tests.py @@ -431,6 +431,14 @@ class PhoneNumberForm(forms.Form): }, ) + @override_settings(PHONENUMBER_DEFAULT_REGION="FR") + def test_formfield_uses_default_region(self): + class PhoneForm(forms.Form): + phone_number = formfields.PhoneNumberField() + + form = PhoneForm() + self.assertEqual("FR", form.fields["phone_number"].region) + class RegionPhoneNumberModelFieldTest(TestCase): def test_uses_region(self):