Fix lost region when PHONENUMBER_DEFAULT_FORMAT
is NATIONAL
#636
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi @stefanfoulis,
Thank you very much for this indispensable library ❤️
I recently noticed a bug when
PHONENUMBER_DEFAULT_FORMAT
is set toNATIONAL
.I initially wanted to open an issue, but when I tried to create a repro, I realized I should open a Pull Request instead.
Description of the bug
When
PHONENUMBER_DEFAULT_FORMAT
is set toNATIONAL
, you used to get a validation error after setting aPhoneNumberField
to a phone number that is not in the default region.This was because
PhoneNumberField
didn't overrideto_python()
, soCharField.to_python()
was called and returnedstr(value)
.Since
str(value)
returns a string in theNATIONAL
format, the region information was lost.Resolution
I fixed this issue by overriding
to_python()
inPhoneNumberField
, ensuring that aPhoneNumber
is returned.This change forced me to move up the call to
super().get_prep_value()
becauseCharField
's implementation callsself.to_python()
, which now returns aPhoneNumber
.This allowed me to remove
if isinstance(value, PhoneNumber)
inget_prep_value()
sincevalue
is now always aPhoneNumber
.Of course, I started with a unit test, which I tried to make as close as possible to the existing ones.
All tests are passing, but the
mypy
job is failing due to an internal error, which was already happening before my changes.Please let me know if you have any questions.
Best Regards,
Benoit