stefanfoulis / django-phonenumber-field

A django model and form field for normalised phone numbers using python-phonenumbers
MIT License
1.49k stars 315 forks source link

Make the model/formfield region kwarg the default selection in PhoneNumberPrefixWidget #574

Closed emdemir closed 10 months ago

emdemir commented 1 year ago

Currently, the region value is passed from the model field to the form field, but it is not used after that. I wanted to make it so that the region value I added in the model field could be also used in the PhoneNumberPrefixWidget as the default region selected, so for example the customer would see their own country selected by default but then they could select another country for international numbers. Would this be an okay addition? Seems like this would simply entail setting self.widget.region = region in the formfield constructor. Alternatively we could pass it as part of the attrs dict, but that would require a custom BoundField subclass as sadly formfields cannot directly influence widget attrs.

I can create a PR if this is acceptable.

francoisfreitag commented 1 year ago

That looks like a usability improvement, thanks for the suggestion! :+1:

I see that the form field already passes the region to widgets that support it. It looks like RegionalPhoneNumberWidget relies on it, so the change will likely be quite small.

https://github.com/stefanfoulis/django-phonenumber-field/blob/60b5886eb45542dfba3de496225a8d3d37525e59/phonenumber_field/formfields.py#L30