@@ -45,14 +45,16 @@ def localized_choices(language):
45
45
class PhonePrefixSelect (Select ):
46
46
initial = None
47
47
48
- def __init__ (self , initial = None , attrs = None ):
48
+ def __init__ (self , initial = None , attrs = None , choices = None ):
49
49
language = translation .get_language () or settings .LANGUAGE_CODE
50
- choices = localized_choices (language )
50
+ if choices is None :
51
+ choices = localized_choices (language )
52
+ choices .sort (key = lambda item : item [1 ])
51
53
if initial is None :
52
54
initial = getattr (settings , "PHONENUMBER_DEFAULT_REGION" , None )
53
55
if initial in REGION_CODE_TO_COUNTRY_CODE :
54
56
self .initial = initial
55
- super ().__init__ (attrs = attrs , choices = sorted ( choices , key = lambda item : item [ 1 ]) )
57
+ super ().__init__ (attrs = attrs , choices = choices )
56
58
57
59
def get_context (self , name , value , attrs ):
58
60
attrs = (attrs or {}).copy ()
@@ -67,9 +69,23 @@ class PhoneNumberPrefixWidget(MultiWidget):
67
69
- an input for local phone number
68
70
"""
69
71
70
- def __init__ (self , attrs = None , initial = None , country_attrs = None , number_attrs = None ):
72
+ def __init__ (
73
+ self ,
74
+ attrs = None ,
75
+ initial = None ,
76
+ country_attrs = None ,
77
+ country_choices = None ,
78
+ number_attrs = None ,
79
+ ):
80
+ """
81
+ :param country_choices: Limit the country choices.
82
+ :type country_choices: list of 2-tuple, optional
83
+ The first element is the value, which must match a country code
84
+ recognized by the phonenumbers project.
85
+ The second element is the label.
86
+ """
71
87
widgets = (
72
- PhonePrefixSelect (initial , attrs = country_attrs ),
88
+ PhonePrefixSelect (initial , attrs = country_attrs , choices = country_choices ),
73
89
TextInput (attrs = number_attrs ),
74
90
)
75
91
super ().__init__ (widgets , attrs )
0 commit comments