Re: Proposal to add attribute 'step' to FloatField and DecimalField

2021-03-22 Thread Kapil Bansal
I do something similar to what math.isclose() provides. Can anyone please review this PR:- https://github.com/django/django/pull/14162 On Mon, Mar 22, 2021, 4:53 PM James Bennett wrote: > There are ways to check "close enough" equality for floats. There's > even the math.isclose() function

Re: Proposal to add attribute 'step' to FloatField and DecimalField

2021-03-22 Thread James Bennett
There are ways to check "close enough" equality for floats. There's even the math.isclose() function which is arbitrarily tune-able: https://docs.python.org/3/library/math.html#math.isclose -- You received this message because you are subscribed to the Google Groups "Django developers

Re: Proposal to add attribute 'step' to FloatField and DecimalField

2021-03-22 Thread Jacob Rief
The problem with modulo on floats is, that due to rounding errors it often created weird results, for instance in Python-3.8 this happens: 3.5 % 0.1 = 0.09981 This btw. also applies to the builtin divmod and math.fmod functions. Therefore I proposed to do it via classic division and

Re: Proposal to add attribute 'step' to FloatField and DecimalField

2021-03-22 Thread 'Adam Johnson' via Django developers (Contributions to Django itself)
Use the modulo operator In [1]: 4.5 % 1.5 Out[1]: 0.0 https://en.wikipedia.org/wiki/Modulo_operation https://docs.python.org/3/reference/expressions.html?highlight=modulo#binary-arithmetic-operations On Sun, 21 Mar 2021 at 23:47, Jacob Rief wrote: > Say, you have a value and step, both are

Re: Proposal to add attribute 'step' to FloatField and DecimalField

2021-03-21 Thread Jacob Rief
Say, you have a value and step, both are floats, then if value / step is can be represented integer, the validation is fulfilled. Otherwise if the result has to be rounded to become an integer, a ValidationError shall be raised. Be aware of rounding errors. On Sunday, March 21, 2021 at 9:53:43

Re: Proposal to add attribute 'step' to FloatField and DecimalField

2021-03-21 Thread Kapil Bansal
Hi everyone, I am working on this and I have a question. How to write validation check for this? Due to python floating point issues, I am not able to validate whether field value is of given step_size or not - Kapil On Wednesday, March 17, 2021 at 9:34:59 PM UTC+5:30 jacob...@gmail.com wrote:

Re: Proposal to add attribute 'step' to FloatField and DecimalField

2021-03-17 Thread Jacob Rief
Great News! So please accept ticket #32559 . I then will assign myself to it and implement it. So I add step (or would you prefer step_value?) to IntegerField and FloatField, but not to DecimalField (because there it's handled through

Re: Proposal to add attribute 'step' to FloatField and DecimalField

2021-03-17 Thread 'Adam Johnson' via Django developers (Contributions to Django itself)
I agree that it makes sense to add the argument to FloatField so we can add the server-side validation logic. I would also like to see it on IntegerField for consistency, and since IntegerField also maps to a NumberInput. I'd also be against the widget_attributes proposal - there are already too

Re: Proposal to add attribute 'step' to FloatField and DecimalField

2021-03-17 Thread Carlton Gibson
> On 17 Mar 2021, at 15:34, Jacob Rief wrote: > > But FloatField also offers a min_value and max_value. When rendered as a > widget, they are used as attributes min and max in their input > field. In addition to that, the field value is validated against a value in > that range. To be

Re: Proposal to add attribute 'step' to FloatField and DecimalField

2021-03-17 Thread Jacob Rief
> > We set maxlength/minlength on widgets for CharFields *because they map > from an already existing kwarg*. > > For the case of `step` on a FloatField we’d need to add a kwarg — but > that’s only there to set a single attribute on the widget. > > But FloatField also offers a min_value and

Re: Proposal to add attribute 'step' to FloatField and DecimalField

2021-03-17 Thread Carlton Gibson
Hiya, DecimalField already sets step based on the number of decimal places def test_decimalfield_widget_attrs(self): f = DecimalField(max_digits=6, decimal_places=2) self.assertEqual(f.widget_attrs(Widget()), {}) self.assertEqual(f.widget_attrs(NumberInput()),

Re: Proposal to add attribute 'step' to FloatField and DecimalField

2021-03-17 Thread Josh Smeaton
Just to clarify - you're talking about form fields rather than model fields, right? I can see the argument for form fields but not for model fields. Is there a better API we can think of for customising widgets from the field constructor that could then be passed through? As a rough example:

Re: Proposal to add attribute 'step' to FloatField and DecimalField

2021-03-17 Thread Jacob Rief
On Wednesday, March 17, 2021 at 12:49:48 AM UTC+1 in...@markusholtermann.eu wrote: > That sounds like a sensible feature. Do you want to open a ticket and > maybe implement it? > Hi Markus, ticket #32559 has been issued to propose this feature. If

Re: Proposal to add attribute 'step' to FloatField and DecimalField

2021-03-16 Thread Markus Holtermann
Hi Jacob, That sounds like a sensible feature. Do you want to open a ticket and maybe implement it? Cheers Markus On Wed, Mar 17, 2021, at 12:45 AM, Jacob Rief wrote: > If someone wants to use the step attribute as provided by the HTML field > , she/he has to specify that using for instance

Proposal to add attribute 'step' to FloatField and DecimalField

2021-03-16 Thread Jacob Rief
If someone wants to use the step attribute as provided by the HTML field , she/he has to specify that using for instance FloatField(widget=NumberInput(attrs={'step': 0.5})). Since the HTML standard offers a step attribute on input fields of type number, from my point of view, this feature shall