-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Add localization support to the FloatField #4925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add localization support to the FloatField #4925
Conversation
Merge master from official django-rest-framework
…FloatField like it is done for the DecimalField, in order to use the user current local to serialise and deserialise the float value if required.
rest_framework/fields.py
Outdated
@@ -939,7 +945,12 @@ def __init__(self, **kwargs): | |||
|
|||
def to_internal_value(self, data): | |||
|
|||
if isinstance(data, six.text_type) and len(data) > self.MAX_STRING_LENGTH: | |||
data = smart_text(data).strip() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not keen on this. For the default behavior we'd now be unnecessarily transforming the incoming value into a string, and then back to a float again. Perhaps we could push this inside the if self.localize:
block?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No because we could have a string as input without having the localize attribute to True.
I'll propose a change doing all string related sanitization only if the input is a string.
Looking good. One comment to address. |
And apologies for the delay! This one seems to have slipped through the cracks for a bit. |
* Do string sanitization only if input is a string
I had a bit of time to rework on this :) Then now the string related stuff are done only if the input of
Thank you for the review, this stuff starts to look good. |
@kgeorgy I've milestoned this for 3.7.0. I'll review it, and we'll either have it or not for then. Any chance you could rebase? Thanks for the input! |
@carltongibson Done! |
docs/api-guide/fields.md
Outdated
- `max_value` Validate that the number provided is no greater than this value. | ||
- `min_value` Validate that the number provided is no less than this value. | ||
- `localize` Set to `True` to enable localization of input and output based on the current locale. This will also force `coerce_to_string` to `True`. Defaults to `False`. Note that data formatting is enabled if you have set `USE_L10N=True` in your settings file. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Defaults to
False
Do you mean None
? It's a boolean field right? Should the default be False
, rather than None
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because it is the way it is in done in the DecimalField, and this pull request is just to apply the exact same principle to FloatField, but it is OK for me to use False as default.
rest_framework/fields.py
Outdated
@@ -954,6 +956,10 @@ class FloatField(Field): | |||
def __init__(self, **kwargs): | |||
self.max_value = kwargs.pop('max_value', None) | |||
self.min_value = kwargs.pop('min_value', None) | |||
self.localize = kwargs.pop('localize', False) | |||
self.coerce_to_string = kwargs.pop('coerce_to_string', None) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not False
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same reason as in the previous point. But we can change both.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you just make this False
(and in the docs signature too) — I think it's cleaner. Thanks!
rest_framework/fields.py
Outdated
self.localize = kwargs.pop('localize', False) | ||
self.coerce_to_string = kwargs.pop('coerce_to_string', None) | ||
if self.localize: | ||
self.coerce_to_string = True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need this here?
In to_representation
:
if self.localize:
return number_format(value)
if self.coerce_to_string:
...
So if self.localize
, self.coerce_to_string
is never tested.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, because if localize is True, we always coerce to string (number_format returns a string). We only needs to check coerce_to_string if localize is False. (And we override coerce_to_string to True in the init, so when localize is True, coerce_to_string must be True).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So we can drop this line...? (We don't need to override coerce_to_string
since it will never be used.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could be used if we don't set localize to True, we could have localize = False and coerce_to_string = True.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure... Line 960 is fine. This comes into play in to_representation
when localize
is `False.
But line 961 and 962 are no-ops really (right?) — So (I think) we can delete them. Yes/no?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok got it... Yes it seems to... just come from a copy & past from line 989 and 990 in DecimalField.init. I'll remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kgeorgy I just have a couple of questions. (See comments) but looks good!
… be False insteed of None
@carltongibson coerce_to_string is now defaulted to False (as it was documented), and the unneeded overriding lines are now removed. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. Awesome work. Thanks!
Description
Full description of the issue can be found here: Issue #4584.
The goals of this pull request is add the localize and coerce_to_string parameters, which are available with the DecimalField, to the FloatField.
This pull request is a simplification of the pull request #4586 as recommended by the maintainer.
refs #4584