diff --git a/phonenumber_field/widgets.py b/phonenumber_field/widgets.py index 60fdf377..c11b59b3 100644 --- a/phonenumber_field/widgets.py +++ b/phonenumber_field/widgets.py @@ -79,6 +79,7 @@ def __init__( country_attrs=None, country_choices=None, number_attrs=None, + region=None, ): """ :keyword dict attrs: See :attr:`~django.forms.Widget.attrs` @@ -92,9 +93,15 @@ def __init__( The second element is the label. :keyword dict number_attrs: The :attr:`~django.forms.Widget.attrs` for the local phone number :class:`~django.forms.TextInput`. + :keyword str region: 2-letter country code as defined in ISO 3166-1. + When not supplied, defaults to :setting:`PHONENUMBER_DEFAULT_REGION` """ widgets = ( - PhonePrefixSelect(initial, attrs=country_attrs, choices=country_choices), + PhonePrefixSelect( + initial or region, + attrs=country_attrs, + choices=country_choices, + ), TextInput(attrs=number_attrs), ) super().__init__(widgets, attrs) diff --git a/tests/test_widgets.py b/tests/test_widgets.py index afea369e..988d50c5 100644 --- a/tests/test_widgets.py +++ b/tests/test_widgets.py @@ -67,6 +67,11 @@ def test_uses_default_region_as_initial(self): self.assertIn('', rendered) self.assertIn('', rendered) + def test_uses_kwarg_region_as_prefix(self): + rendered = PhoneNumberPrefixWidget(region="CN").render("", "") + self.assertIn('', rendered) + self.assertIn('', rendered) + def test_no_initial(self): rendered = PhoneNumberPrefixWidget().render("", "") self.assertIn('', rendered)